public static void CopyFile(File file1,File file2)throws IOException{
if(!file1.exists()){
throw new IllegalArgumentException("文件"+file1+"不存在");
}
if(!file1.isFile()){
throw new IllegalArgumentException(file1+"不是文件");
}
FileInputStream in = new FileInputStream(file1);
FileOutputStream out = new FileOutputStream(file2);
byte[]buf = new byte[8*1024];
int b;
while((b = in.read(buf, 0 , buf.length))!=-1){
out.write(buf, 0, b);
out.flush();
}
in.close();
out.close();
}
package javaIO;
import java.io.File;
import java.io.IOException;
public class IOUtiltest3 {
public static void main(String[] args) {
try {
IOutil.CopyFile(new File("f\\123.txt"), new File("f\\456.txt"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}