Home Tutorial Java Core Files Java file DataInputStream

 
 

Java file DataInputStream
Posted on: June 10, 2006 at 12:00 AM
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

Related Tags for Java file DataInputStream:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.