Very easy way to open and read any type of file in Java

How I can open a file in Java, read it and print its content



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.io.File;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import javax.swing.JOptionPane;

public class FileReader {
    public static void main(String[] args) {
        File file = new File("C:\\Tasks.txt");
        try{
            BufferedInputStream bis;
            DataInputStream dis;
            try (FileInputStream fis = new FileInputStream(file)) {
                bis = new BufferedInputStream(fis);
                dis = new DataInputStream(bis);
                while(dis.available() != 0)
                    System.out.println(dis.readLine());
            }
            bis.close();
            dis.close();
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
    }
    
}





Comments