How to open and read information from a text file with Java

In this tutorial I will show you an easy way to read a text file by using Java language. I have created a text file called ab.txt.

I have a simple Java class:

import java.io.*;
public class Main {
public static void main(String args[]) throws IOException{

}

}

I will use the File and FileInputStream classes to open the text file:

File file = new File("G:/Users/Computer/workspace/New/ab.txt");
FileInputStream file2 = null;
file2 = new FileInputStream(file);

Next, I will use while to read the content:

int content;
while((content = file2.read()) != -1)
{
System.out.print((char) content);
}

Video tutorial:









Leave a Comment