public class CopyFile {
/**
* @throws FileNotFoundException
* @拷贝文件
*/
public static void copyFile(File s,File d) throws IOException{
FileInputStream fis=new FileInputStream(s);
FileOutputStream fos=new FileOutputStream(d);
byte[]buf=new byte[1024];
int len=0;
try {
while((len=fis.read(buf))!=-1){
fos.write(buf,0,len) ;
}
} catch (IOException e) {
throw new RuntimeException("文件复制失败");
}
finally{
try{
if (fis!=null)
fis.close();
}
catch(IOException e){
throw new RuntimeException("文件读取未正常关闭");
}
}
}
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
File s=new File("F:\\1.java");
File dir=new File("g:\\1");
if (!dir.exists()){
dir.mkdir();
}
// File d=new File(dir,"FileCopy.jad");
File d=new File(dir,"d:_java_test_src_Hello.jad");
copyFile(s, d);
System.out.println("复制成功!");
}
}
大家一起分析下:为什么文件名中包含了“:”,程序也正常运行了,拷贝出一个0字节的文件,名字只有“d”!
把“:”去掉就正常了!~ |
|