Advertisement
yo2man

Tutorial 38 Reading Files with the File Reader

May 24th, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.83 KB | None | 0 0
  1. //Tutorial 38 Reading Files with the File Reader
  2. //App.java
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8.  
  9. //You can read files using the scanner class(see previous tutorial 33) which is a bit less cumbersome,
  10. //however, this is still important because you can change the code/adapt it so it will read files of all different formatss like the binary format.
  11. //so what we are going to write here is very flexible despite cumbersome.
  12. public class App {
  13.     public static void main(String[] args) {
  14.         File file = new File("text.txt"); //step 1.create new file
  15.        
  16.         BufferedReader br = null;
  17.        
  18.         try {
  19.             FileReader fr = new FileReader(file);
  20.             br = new BufferedReader(fr); //5. Do Buffering, right click on error, import what you need
  21.            
  22.             String line; //
  23.            
  24.             ///line = br.readLine();  //then click on error and add catch clause surround with try catch
  25.                                                     //6. want a while loop so the App reads every line in the text.
  26.             while( (line = br.readLine())!= null ){ //   want the while loop to stop reading when their are no more lines: "null"
  27.             System.out.println(line);
  28.             }
  29.            
  30.            
  31.            
  32.         } catch (FileNotFoundException e) {
  33.             System.out.println("File not found: " + file.toString());
  34.         } //2. read the file with FileReader
  35.                                               //3.(ctrl+shift+O ) to import "import java.io.File; import java.io.FileReader;"
  36.                                               ///it will throw a filenotfound exception, click the error and "surround it with trycatch"   
  37.         catch (IOException e) {
  38.             System.out.println("unable to read file: " + file.toString());
  39.            
  40.         }
  41.         finally { //finally block is always going to be executed everytime, even if exceptions are thrown
  42.             try {
  43.                 br.close();
  44.             } catch (IOException e) {
  45.                 System.out.println("unable to close file: " + file.toString());
  46.             }
  47.             catch(NullPointerException ex){
  48.                 //File was probably never opened!
  49.             }
  50.         }
  51.     }
  52.  
  53. }
  54. //---------------------------------------------------------------------------------------------------------------------------------------
  55. //text.txt
  56. first line
  57. second line
  58. third line
  59. //---------------------------------------------------------------------------------------------------------------------------------------
  60. /*Run Results:
  61. first line
  62. second line
  63. third line
  64. */
  65.  
  66. //---------------------------------------------------------------------------------------------------------------------------------------
  67. /*
  68. Error Message (if u purposely get an error by switching the name of the text[ie: tefxt.txt) so it the program can't find the file):
  69.  
  70. File not found: tefxt.txt
  71. */
  72.  
  73.  
  74. //note: there is a language feature in Java.7 that can simplify this exceptions nested mess: we will go over it in the next tutorial.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement