How to read file in Java – BufferedReader
In Java, there are many ways to read a file, here we show you how to use the simplest and most common-used method – BufferedReader.
package com.mkyong.io; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class BufferedReaderExample { public static void main(String[] args) { BufferedReader br = null; try { String sCurrentLine; br = new BufferedReader(new FileReader("C:\\testing.txt")); while ((sCurrentLine = br.readLine()) != null) { System.out.println(sCurrentLine); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null)br.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }
See updated example in JDK 7, which use try-with-resources new feature to close file automatically.
package com.mkyong.io; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class BufferedReaderExample { public static void main(String[] args) { try (BufferedReader br = new BufferedReader(new FileReader("C:\\testing.txt"))) { String sCurrentLine; while ((sCurrentLine = br.readLine()) != null) { System.out.println(sCurrentLine); } } catch (IOException e) { e.printStackTrace(); } } }

HE HE HE HI
thanks you !
Hi, your codes works fine but i am getting an encrypted version, so how can i get non-encrypted version.
Hi.
Thank you.
Good bye.
Hi, Goodbye, Thankyou, what the heck???
:( otherwise known as :(((
How can you adapt the code to allow the variable “sCurrentline”, to be manipulated through methods such as, .split(), out side of the try-catch statement. It will not even print “sCurrentLine”, unless it is in side the try-catch statement.
Hi, where the txt file should be placed??
I created a txt file on the same folder of the java files and the application always throw the FileNotFoundException. I tried different names, different format files, but it never finds the file.
Thanks
you can save it anywhere but you have to save it with .java extension
The entire Java File Operation like Reading, writing, Delete are found here,
http://antguider.blogspot.in/2012/06/java-file-operation.html
Finally after much searching, you answered my question, Mike !
Thanks !
There are a lot of people out in the web asking the same question.
When they and, (up till a few moments ago), myself included, try to “import” a simple text file into an Eclipse project, we were all receiving “file not found”.
Being new to Java I was amazed that this was such an “issue” for eclipse. I like Eclipse but something so fundamental, shouldn’t be so frustratingly hard. I guess the Devs are busy working on other more urgent issues and we cant complain as it is a free and in reality a good product.
Many thanks ! (c:\\testing) :)
somebody help this problem …
how to read this file 1
23
45
In your example, you should make sure to close the BufferedReader, otherwise the file may be lock not readable by some other process.
so
….
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
br.close();
…..
Example is updated, with new JDK7 example.
sir i want jtree tool coding for netbeans 6.9
He he HI!