按照以下代码的方式在生成newFile 之后,如果newFile为Excel文件,那么会出现 “文件格式错误,由Excel进行尽量修复”,并且能够修复成功。
FileInputStream fis = new FileInputStream(oldFile);
FileOutputStream fos = new FileOutputStream(newFile);
byte[] buffer = new byte[1024];
int len;
while((len = fis.read(buffer)) > 0) {
fos.write(buffer);
}
...
之所有出现这个问题是因为 在读取最后一个buffer时,可能fis的长度没有1024,程序默认使用 00填充
,
修改为下面代码:
FileInputStream fis = new FileInputStream(oldFile);
FileOutputStream fos = new FileOutputStream(newFile);
byte[] buffer = new byte[1024];
while(fis.available() >= 1024 && (fis.read(buffer)) > 0) {
fos.write(buffer);
}
buffer = new byte[fis.available()];
fis.read(buffer);
fos.write(buffer);