我将.docx文件读取到字节数组,但当我尝试将其再次保存到.docx文件时,该文件无法打开。
任何人都可以解释原因吗?
public static void main(String[] args) throws IOException {
byte[] data = readFile("test.docx");
System.out.println(data.length);
try (FileOutputStream fos = new FileOutputStream("testCopy.docx")) {
fos.write(data);
}
}
这是readFile方法
public static byte[] readFile(String filePath) throws FileNotFoundException, IOException {
File file = new File(filePath);
byte[] bytes = new byte[(int) file.length()];
try (DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath)))) {
dataInputStream.readFully(bytes);
}
return bytes;
}
答案 0 :(得分:0)
您应该查看DataInputStream和FileInputStream之间的区别。
尝试使用FileInputStream而不是DataInputStream,看看它是如何工作的。
try (FileInputStream fileInputStream = new FileInputStream(filePath)) {
fileInputStream.read(bytes);
}