单个文件复制
FileInputStream input=new FileInputStream("C://360//fay.jpg");
FileOutputStream output=new FileOutputStream("D://Test//fay2.jpg");
byte[] bytes=new byte[6];
int sum=0;
while (input.read(bytes)==bytes.length){
output.write(bytes);
sum++;
}
output.write(bytes);
System.out.println(sum);
input.close();
output.close();
整个文件夹(包含文件夹中的所有内容)复制
import java.io.*;
public class Homework2 {
public static void main(String[] args) throws IOException {
String path="D:\Copy"; \复制到哪个路径(path)中
File file=new File("D:\Test");
copyAll(file,path);
}
public static void copy(File f,String path) throws IOException {
FileInputStream in = new FileInputStream(f);
FileOutputStream out = new FileOutputStream(path+f.getName());
byte[] bytes=new byte[512];
while ((in.read(bytes))!=-1){
out.write(bytes,0,bytes.length);
}
}
public static void copyAll(File file,String path) throws IOException {
File outFile=new File(path);
outFile.mkdirs();
File[] filelists=file.listFiles();
for (File f:filelists) {
if(f.isFile()){
copy(f,path+File.separator);
}else {
copyAll(f,outFile.getPath()+File.separator+f.getName());
}
}
}
}