日志文章

2019-12-23 aflfte2011

利用IO流实现文件的拷贝

package com.aflfte.IO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 文本文件拷贝
 * @author jinhao
 *
 */
public class Test06 {
public static void main(String[] args) {
Copy("src/abc.txt","src/a.txt");

}
public static void Copy(String inpu,String outpu) {
File f=new File(inpu);
File ft=new File(outpu);
OutputStream ou=null;
InputStream in=null;
try {
in=new FileInputStream(f);
ou=new FileOutputStream(ft,true);
byte[] inb=new byte[1024];
int len=-1;
while((len=in.read(inb))!=-1) {
ou.write(inb,0,len);
ou.flush();
}
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
if(ou!=null) {
try {
ou.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
if(in!=null) {
try {
in.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
}


« IO写入文件 | IO读取文件内容»