/*
Read byte array from file using DataInputStream
This Java example shows how to read an array of bytes from file using
read or readFully method of Java DataInputStream class.
*/
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ReadByteArrayFromFile {
public static void main(String[] args) {
String strFilePath = "C://FileIO//readByteArray.txt";
try
{
//create FileInputStream object
FileInputStream fin = new FileInputStream(strFilePath);
/*
* To create DataInputStream object, use
* DataInputStream(InputStream in) constructor.
*/
DataInputStream din = new DataInputStream(fin);
/*
* To read an array of bytes from file, use
* int read(byte b[]) method of Java DataInputStream class.
*
* This method reads bytes from input stream and store them in array of bytes.
* It returns number of bytes read.
*/
byte b[] = new byte[10];
din.read(b);
/*
* To close DataInputStream, use
* void close() method.
*/
din.close();
}
catch(FileNotFoundException fe)
{
System.out.println("FileNotFoundException : " + fe);
}
catch(IOException ioe)
{
System.out.println("IOException : " + ioe);
}
}
}
Bookmark/Search this post with: