By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,538 Members | 1,130 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,538 IT Pros & Developers. It's quick & easy.

Readinf from files in java

P: 45
Hi,

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

thanks
Jan 8 '07 #1
Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
Hi,

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

thanks



Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.io.*;
  3. public class ReadFile {
  4.  public static void main(String args[]) {
  5.   String line = "";
  6.   try {
  7.    FileReader fr = new FileReader("test.txt");
  8.    BufferedReader br = new BufferedReader(fr);
  9.    while((line = br.readLine()) != null) {
  10.     System.out.println(line);
  11.    }
  12.   }
  13.   catch(FileNotFoundException fN) {
  14.    fN.printStackTrace();
  15.   }
  16.   catch(IOException e) {
  17.    System.out.println(e);;
  18.   }
  19.  }
  20. }
Will read a file called test.txt that is saved in the folder that contains your java file
Jan 8 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.