67,513
社区成员
发帖
与我相关
我的任务
分享
public class Copypicture {
public static void main(String[] args) {
copy_2();//copy_1();
}
public static void copy_1(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("1.doc");
fos = new FileOutputStream("2.doc");
byte[] buff = new byte[1024];
int len = 0;
while((len=fis.read(buff))!=-1){
//System.out.println(len);
fos.write(buff,0,len);
}
System.out.println("success!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void copy_2(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
//int i = 0;
try {
bis = new BufferedInputStream(new FileInputStream("1.doc"));
bos = new BufferedOutputStream(new FileOutputStream("2.doc"));
//byte[] buff = new byte[1024];
int len = 0;
while((len=bis.read())!=-1){//return getBufIfOpen()[pos++] & 0xff;
//System.out.println(i++);
bos.write(len);//文件有多字节就写多少次
}
System.out.println("success!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
bis.close();
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}