【文件属性】:
文件名称:java 文件复制
文件大小:1KB
文件格式:JAVA
更新时间:2017-06-06 11:29:46
java 复制
利用java io实现文件复制
public class FileCopy {
public boolean copyFile (String srcPath, String desPath) throws FileNotFoundException{
File srcFile,desFile;
srcFile=new File (srcPath);
desFile=new File (desPath);
FileInputStream fis=null;
FileOutputStream fos=null;
byte[] buf=new byte[8*1024];
int nbyte;
try {
desFile.createNewFile();
fis=new FileInputStream (srcFile);
fos=new FileOutputStream (desFile);
while((nbyte=fis.read(buf))!=-1){
fos.write(buf, 0, nbyte);
}
fos.flush();
fos.close();
fis.close();
}
catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public static void main(String[] args) {
String src = args[0];
String des = args[1];
FileCopy fc=new FileCopy();
try {
if(fc.copyFile(src,des))
System.out.println("文件复制成功!");
else
System.out.println("文件复制失败!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}