Java直接读取一个文件,然后将读取的内容写入另一个文件,文件内容发生变化
代码如下:publicvoidtest(){byte[]content=getBytesFromStream(newFileInputStream("E:\\test.p...
代码如下:
public void test() {
byte[] content = getBytesFromStream(new FileInputStream("E:\\test.pdf");
write((new String(content)).getBytes(), "E:\\output.pdf");
}
public byte[] getBytesFromStream(InputStream inputStream) throws Exception {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int readLen = 0;
while ((readLen = inputStream.read(buffer)) > 0) {
output.write(buffer, 0, readLen);
}
output.flush();
return output.toByteArray();
}
public void write(byte[] input , String filePath) throws Exception {
ByteArrayInputStream inputStream = new ByteArrayInputStream(input);
FileOutputStream output = new FileOutputStream(filePath);
byte[] buffer = new byte[1024];
int readLen = 0;
while ((readLen = inputStream.read(buffer)) > 0) {
output.write(buffer, 0, readLen);
}
output.flush();
output.close();
}
运行以后,发现test.pdf与output.pdf有个别内容不一样,图片是两者16进制对比,红色为不同处
现在已经发现文本是在(new String(content)).getBytes()这一步发生了变化,
如果直接把content传入就OK,但为什么这一步会发生变化呢? 展开
public void test() {
byte[] content = getBytesFromStream(new FileInputStream("E:\\test.pdf");
write((new String(content)).getBytes(), "E:\\output.pdf");
}
public byte[] getBytesFromStream(InputStream inputStream) throws Exception {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int readLen = 0;
while ((readLen = inputStream.read(buffer)) > 0) {
output.write(buffer, 0, readLen);
}
output.flush();
return output.toByteArray();
}
public void write(byte[] input , String filePath) throws Exception {
ByteArrayInputStream inputStream = new ByteArrayInputStream(input);
FileOutputStream output = new FileOutputStream(filePath);
byte[] buffer = new byte[1024];
int readLen = 0;
while ((readLen = inputStream.read(buffer)) > 0) {
output.write(buffer, 0, readLen);
}
output.flush();
output.close();
}
运行以后,发现test.pdf与output.pdf有个别内容不一样,图片是两者16进制对比,红色为不同处
现在已经发现文本是在(new String(content)).getBytes()这一步发生了变化,
如果直接把content传入就OK,但为什么这一步会发生变化呢? 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询