Java file DataInputStream


 

Java file DataInputStream

This section demonstrates you the use of DataInputStream class.

This section demonstrates you the use of DataInputStream class.

Java file DataInputStream

This section demonstrates you the use of DataInputStream class.

The class DataInputStream allows to read primitive Java data types from an underlying input stream. It reads the file line by line. In the given example, we have created an instance of DataInputStream class which wraps the FileInputStream. The method available() returns 0 if the file does not have more lines. The method readLine() reads the line from the file.

Here is the code:

import java.io.*;

public class FileDataInputStream {
	public static void main(String[] args) throws Exception {
		File file = new File("C:/data.txt");
		DataInputStream dis = new DataInputStream(new FileInputStream(file));
		while (dis.available() != 0) {
			System.out.println(dis.readLine());
		}
		dis.close();
	}
}

Through the above code, you can read any file using DataInputStream.

Output:

Hello

All glitters are not gold

Ads