We will read c:/myFile.txt
c:/myFile.txt looks like this >
You are learning File IO from javaMadeSoEasy.com
|
Program to Read file in byteArray using FileInputStream in java file IO >
import java.io.FileInputStream;
import java.io.IOException;
/** JavaMadeSoEasy.com */
public class ReadFileInByteArray {
public static void main(String... args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("c:/myFile.txt");
//create byteArray of file's size
byte[] bytes = new byte[fis.available()];
fis.read(bytes); //Read file in ByteArray
//Now, let's display byteArray
String str=new String(bytes); //convert ByteArray to String
System.out.println("byteArray formed from File = "+str);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close(); //close FileInputStream
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*OUTPUT
byteArray formed from File = You are learning File IO from javaMadeSoEasy.com
*/
|
RELATED LINKS>
Write byteArray to file using FileOutputStream in java file IO
Labels:
Core Java
File IO/File handling