太不巧4119867
2016-12-07 14:43
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();
}
}
}
创建的不是在f盘里,这样创建才是在f盘里:(new File("f:\\123.txt")。
(new File("f\\123.txt"), 这样是在工程路径下创建的需要按f5刷新,就会出现。
文件传输基础——Java IO流
133846 学习 · 1060 问题
相似问题