Techtopz

(@techtopz)

Techtopz is a blog on programming.
Follow us on here for more updates.

    


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.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
 
// 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
          String a;
          //read
          a = r.readLine();
          //print the string.
          System.out.println("The txt file contains:-");
          System.out.println(a);
       }
}


Output

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.

Explanation.
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.


453 likes

Share:     Share to Facebook    Share to Twitter    Share to WhatsApp    Share to Telegram
Login to follow and get updates from @techtopz