Java examples for File Path IO:File Stream
To read a binary file, you usually work with the following classes:
Use the File class to represent the file itself.
FileInputStream connects the input stream to a file.
BufferedInputStream adds buffering to the basic FileInputStream.
DataInputStream reads data from the stream.
The other Stream classes read a byte at a time.
DataInputStream class reads basic data types, including primitive types and strings.
The following code shows how to create a DataInputStream.
import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; public class Main { public static void main(String[] args) throws FileNotFoundException { File file = new File("movies.dat"); DataInputStream in = new DataInputStream( new BufferedInputStream(new FileInputStream(file))); }//from w w w . ja va 2s.c o m }
Or you can do step by step.
import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; public class Main { public static void main(String[] args) throws FileNotFoundException { File file = new File("movies.dat"); FileInputStream fs = new FileInputStream(file); BufferedInputStream bs = new BufferedInputStream(fs); DataInputStream in = new DataInputStream(bs); } }