Home Tutorial Java Core Files Java file DataInputStream

 
 

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

Advertisement

Related Tags for Java file DataInputStream:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 10, 2006

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com