为什么当我尝试使用任何Writer实现来复制Excel文件的内容时,文件的内容被破坏! 。将xls文件从FileInputStream复制到FileWriter会破坏文件
@Test
public void testExcelCopy1() throws FileNotFoundException, IOException {
IOUtils.copy(new FileInputStream(new File("d:\\temp\\123.xls")), new OutputStreamWriter(
new FileOutputStream("d:\\temp\\1234.xls")));
Assert.assertArrayEquals(FileUtils.readFileToByteArray(new File("d:\\temp\\123.xls"))
, FileUtils.readFileToByteArray(new File("d:\\temp\\1234.xls")));
}
@Test
public void testExcelCopy2() throws FileNotFoundException, IOException {
IOUtils.copy(new FileInputStream(new File("d:\\temp\\123.xls")), new FileOutputStream("d:\\temp\\12345.xls"));
Assert.assertArrayEquals(FileUtils.readFileToByteArray(new File("d:\\temp\\123.xls"))
, FileUtils.readFileToByteArray(new File("d:\\temp\\12345.xls")));
}
我有一个reqiurement凡在我需要与Apache POI API创建的工作簿写入一个作家的实现!和我坚持这个问题!任何人都可以帮忙吗?
更新:
我明白的问题是,当我试图将一个字节流转换为字符流。但我不明白为什么。
此测试也失败!
@Test
public void testExcelCopy1() throws FileNotFoundException, IOException {
Reader reader = new InputStreamReader(new FileInputStream("d:\\temp\\123.xls"));
Writer write = new OutputStreamWriter(new FileOutputStream("d:\\temp\\1234.xls"));
IOUtils.copy(reader, write);
write.flush();
write.close();
Assert.assertArrayEquals(FileUtils.readFileToByteArray(new File("d:\\temp\\123.xls"))
, FileUtils.readFileToByteArray(new File("d:\\temp\\1234.xls")));
}
因为你试图写一个字符为基础的API – Sanjeev
二进制流这是否意味着没有办法,我可以做到这一点! 。我想将一个excel内容的字节数组写入Writer实现中。有没有其他方法?! 。 – Sagar
您应该使用二进制流API来写入二进制数据。为什么需要将读写器的实现放在上面呢 – Sanjeev