brainfrz

Untitled

Sep 24th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. /**************************************************************************************************
  2.  *  Program Name:     File Reader
  3.  *  Author:           Terry Weiss
  4.  *  Date Written:     September 24, 2015
  5.  *  Course/Section:   CSC 111-003W
  6.  *  Program Description:
  7.  *     This program will read and display a file.
  8.  **************************************************************************************************/
  9.  
  10. package filereader;
  11.  
  12. import java.util.Scanner;
  13. import java.io.File;
  14. import java.io.IOException;
  15.  
  16.  
  17. /**
  18.  *
  19.  * @author Terry
  20.  */
  21. public class FileReader
  22. {
  23.     /**
  24.      *
  25.      * @param file the Scanner object holding the contents of the file being read
  26.      * @return A String holding the contents of the file
  27.      */
  28.     public static String readFile( Scanner file )
  29.     {
  30.         String fileStr = "";
  31.        
  32.         while (file.hasNext())
  33.         {
  34.             fileStr += file.nextLine() + "\n";
  35.         }
  36.        
  37.         return fileStr;
  38.     }
  39.    
  40.    
  41.    
  42.     /**
  43.      * @param args the command line arguments
  44.      * @throws IOException
  45.      */
  46.     public static void main( String[] args ) throws IOException
  47.     {
  48.         Scanner file = new Scanner(new File("file.txt"));
  49.        
  50.         System.out.println("Now reading file.txt:\n");
  51.        
  52.         System.out.println(readFile(file));
  53.     }
  54.    
  55. }
Advertisement
Add Comment
Please, Sign In to add comment