如何在Java中将文件内容写入字节数组?
FileInputStream类包含一个方法read(),该方法接受字节数组作为参数,并将文件输入流的数据读取到给定的字节数组。
示例
import java.io.File;
import java.io.FileInputStream;
public class FileToByteArray {
public static void main(String args[]) throws Exception {
File file = new File("HelloWorld");
FileInputStream fis = new FileInputStream(file);
byte[] bytesArray = new byte[(int)file.length()];
fis.read(bytesArray);
String s = new String(bytesArray);
System.out.println(s);
}
}输出结果
//Class declaration
public class SampleProgram {
/* This is my first java program.
This will print '你好世界' as the output
*/
//主要方法
public static void main(String []args) {
//打印你好世界-
System.out.println("你好世界");
}
}热门推荐
10 过年爱情祝福语大全简短
11 女朋友祝福语简短新年
12 新娘上头祝福语大全简短
13 学生宴祝福语简短的
14 公司开年仪式祝福语简短
15 弟弟生日祝福语简短的话
16 关于好的祝福语简短
17 20岁简短生日祝福语
18 新婚贺词简短的祝福语