Reading text files in JAVA

I used the below given code to read a text file(data.txt) during one of my course works for JAVA as it was a requirement for that course work. And thought it will be usefull for many others in carrying out various other projects as well.

Getting into to the code first up you need to import two classes, BufferedReader and FileReader in my case i have used the main method itself to read the file if you wanna read it more than once the recommended way is to introduce a new function to read the file since, it would be the professional approach to get on with it

And you will also note that I have used the split() function to a good use here using an array of string called “tkn” which takes all the required fields from the text file where ever it comes across a (,) in  a particular line. Hence, you can manipulate all those elements seperately later in your programme.

import; public class ReadText
 public static void main(String args[])


  FileReader fread=null;
  BufferedReader bread = new BufferedReader(new FileReader(”data.txt”));

  String line;
   String[] tkn =line.split(“,”);
   for(int i=0; i<tkn.length;i++)



catch(Exception e)
     System.out.println(”Exception: ” + e);

} //end of main()

} //end of class ReadText


3 thoughts on “Reading text files in JAVA

  1. vishal singwall says:

    Too good .. this was what i wanted Bingo… but still there is some work to be done…..

Comments are closed.