拷贝文件
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;
public class TestIo05 {
public static void main(String[] args) {
copyfile("1.jpg","2.jpg");
}
public static void copyfile(String sourcePath,String destPath) {
File sourceSrc = new File(sourcePath);//源文件
File destSrc = new File(destPath);//复制文件
OutputStream os = null;
InputStream is = null;
try {
os = new FileOutputStream(destSrc);
is = new FileInputStream(sourceSrc);
byte[] flush = new byte[1024];
int len = -1;
while ((len = is.read(flush)) != -1) {
os.write(flush,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (is != null) {
is.close();
}
} catch (Exception e2) {
// TODO: handle exception
}
}
}
}