转换具有文本初始化的文件后,我的字节数组充满零的原因是什么?

What is the reason my byte array is full of zeros after converting a file which have text init?

我正在尝试读取文本文件并将这些内容插入数据库。 我的 file.length() 是 3540 但是字节数组充满了零。结果当我打开文本文件时,它是空的。

File file = new File("/temp/abc.txt");
byte[] bytesArray = new byte[(int) file.length()]; 
databaseBean.setContentInByteArray(bytesArray);

这里的字节数组全是零。

Path file = Paths.get("/temp/abc.txt");
byte[] bytesArray = Files.readAllBytes(path); 
databaseBean.setContentInByteArray(bytesArray);

A File 只是磁盘上物理文件的持有者,路径,而不是其内容。

new byte[42] 将创建一个 42 字节的归零数组。

您将不得不阅读那些字节。 Path 是更新的、更通用的 class i.o。 File 并且我使用 Files class 读取所有这些字节。