FileInputStream类包含一个方法read(),该方法接受字节数组作为参数,并将文件输入流的数据读取到给定的字节数组。假设文件myData包含以下数据-
Hi how are you welcome to Nhooo
import java.io.File;
import java.io.FileInputStream;
public class FileToByteArray {
public static void main(String args[]) throws Exception{
File file = new File("myData");
FileInputStream fis = new FileInputStream(file);
byte[] bytesArray = new byte[(int)file.length()];
fis.read(bytesArray);
String s = new String(bytesArray);
System.out.println(s);
}
}输出结果
Hi how are you welcome to Nhooo