我正在尝试读取一个文本文件并将这些内容插入数据库。我的 file.length() 是 3540 但字节数组全是零。结果当我打开文本文件时,它是空的。File file = new File("/temp/abc.txt");byte[] bytesArray = new byte[(int) file.length()]; databaseBean.setContentInByteArray(bytesArray);这里的 byteArray 充满了零。
1 回答

慕妹3242003
TA贡献1824条经验 获得超6个赞
Path file = Paths.get("/temp/abc.txt");
byte[] bytesArray = Files.readAllBytes(path);
databaseBean.setContentInByteArray(bytesArray);
AFile只是磁盘上物理文件的持有者,一个路径,而不是它的内容。
new byte[42]将创建一个 42 字节的归零数组。
您将不得不阅读这些字节。Path是一个更新、更通用的 io 类File,我使用该类Files来读取所有这些字节。
添加回答
举报
0/150
提交
取消