IO流:拷贝文件/文件夹到另一路径
使用InputStream和OutputStream
package com.xuedao.main;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 拷贝文件
* @author 阿超
*
*/
public class FileCopy02 {
public static void main(String[] args) throws IOException {
File file = new File("D:/QQ文件/a.txt");//源文件路径
File tar = new File("D:/QQ文件/java/acopy.txt");//复制的目标路径,复制之后的可任意更名a.txt->acopy.txt
copy(file, tar);
}
// 源文件路径 复制的目标路径
public static void copy(File file,File target) throws IOException {
InputStream in = new FileInputStream(file);
OutputStream out = new FileOutputStream(target);
byte[] b = new byte[1024];
int l = -1;
while((l = in.read(b)) != -1) {
out.write(b, 0, l);
}
in.close();
out.close();
}
}