Readinf from files in java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • outofmymind
    New Member
    • Oct 2006
    • 45

    Readinf from files in java

    Hi,

    Can anyone please teach me a simple way of reading from a file and closing it, etc.

    thanks
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by outofmymind
    Hi,

    Can anyone please teach me a simple way of reading from a file and closing it, etc.

    thanks



    Code:
     
    import java.io.*;
    public class ReadFile {
     public static void main(String args[]) {
      String line = "";
      try {
       FileReader fr = new FileReader("test.txt");
       BufferedReader br = new BufferedReader(fr);
       while((line = br.readLine()) != null) {
    	System.out.println(line);
       }
      }
      catch(FileNotFoundException fN) {
       fN.printStackTrace();
      }
      catch(IOException e) {
       System.out.println(e);;
      }
     }
    }
    Will read a file called test.txt that is saved in the folder that contains your java file

    Comment

    Working...