ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

How to read a plain text file in Java?

In this post we will learn how to read data from a plain text file. There are multiple ways in Java about how to read data and write data from/in a text file. Also multiple third party party applications are present in the market which we can use while dealing with reading or writing operation.

Anyways , let us go through a simple program which demonstrate how to read data from a text file.

  1. public class Manager{
  2.  
  3. public static void main( String [] args) {
  4.  
  5. File f=new File("test.txt")
  6.  
  7. //We are passing path of the file in the argument
  8.  
  9.  
  10. BufferReader bin=new BufferrReader(fr);
  11.  
  12. String s1=bin.readLine(); // reading first line
  13.  
  14. while (s1!=null){
  15.  
  16. System.out.println(s1);
  17.  
  18. s1=bin.readLine();
  19.  
  20. // reading next line. Similarly this loop will read and print all the line
  21.  
  22. }
  23.  
  24. }
  25.  
  26. }

Here readLine() method will read one line and move the cursor to the next line

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!