java复制生成的pdf文件打不开
写了一个小程序,想拷贝生成一个新的pdf文件,pdf文件倒是能生成,但却不能够打开。
不知道原因,请高手赐教!
程序如下:
package check;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import
java.io.FileNotFoundException;
import java.io.FileOutputStream;
import
java.io.IOException;
import java.io.InputStream;
import
java.io.UnsupportedEncodingException;
import java.util.Arrays;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
try{
DataInputStream fin = new DataInputStream(new BufferedInputStream(new FileInputStream("C:\\java.pdf")));
DataOutputStream fout = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("C:\\java2.pdf")));
byte[] array = new byte[10];
while((fin.read(array))!=-1){
fout.write(array);
}
fout.close();
fin.close();
}catch(
FileNotFoundException e)
{
e.printStackTrace();
}catch (
IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
------解决方案--------------------
用BuffedStream就可以了,array定义太小
Java code
byte[] buff = new byte[1024];
int bytesRead;
while(-1 != (bytesRead = fin.read(buff, 0, buff.length))) {
fout.write(buff,0,bytesRead);
}