Java Programming | Reading a Text File.


This is a tutorial for reading string from a txt file. The program reads the string from the file and prints it on the screen. The program is extendable. Go enjoy the program. Lets begin…

Program for Reading a string from a txt file.

//import these as we require them.
// the name of our class its public
public class ReadFile {
    //void main
    public static void main(String args[])throws IOException{
        //declare the BufferedReader obj
        BufferedReader r = new BufferedReader(new FileReader("file.txt"));
          String a;
          a = r.readLine();
          //print the string.
          System.out.println("The txt file contains:-");


The txt file contains:- hello

How does it work

The program reads the string from txt file and prints it.

Extending it

The program can be extended. This is a basic concept of java programming and has lots of applications.


  1. Import those 3 things.
  2. Declare the class as public
  3. Add the void main function
  4. Declare reader object.
  5. Declare a String.
  6. Read the line.
  7. Add system.out.println() function to print the string.

At the end.

You learnt creating the Java program for Reading the txt file. So now enjoy the program.

Please comment on the post and share it.

Join India's Largest Code Learning Community on Get it on Google Play
Sharing options: