java 文件拷贝文件怎么打开_java如何拷贝文件
/*** 復(fù)制單個(gè)文件
*@paramoldPath String 原文件路徑 如:c:/fqf.txt
*@paramnewPath String 復(fù)制后路徑 如:f:/fqf.txt
*@returnboolean*/
public voidcopyFile(String oldPath, String newPath) {try{int bytesum = 0;int byteread = 0;
File oldfile= newFile(oldPath);if (oldfile.exists()) { //文件存在時(shí)
InputStream inStream = new FileInputStream(oldPath); //讀入原文件
FileOutputStream fs = newFileOutputStream(newPath);byte[] buffer = new byte[1444];intlength;while ( (byteread = inStream.read(buffer)) != -1) {
bytesum+= byteread; //字節(jié)數(shù) 文件大小
System.out.println(bytesum);
fs.write(buffer,0, byteread);
}
inStream.close();
}
}catch(Exception e) {
System.out.println("復(fù)制單個(gè)文件操作出錯(cuò)");
e.printStackTrace();
}
}/*** 復(fù)制整個(gè)文件夾內(nèi)容
*@paramoldPath String 原文件路徑 如:c:/fqf
*@paramnewPath String 復(fù)制后路徑 如:f:/fqf/ff
*@returnboolean*/
public voidcopyFolder(String oldPath, String newPath) {try{
(new File(newPath)).mkdirs(); //如果文件夾不存在 則建立新文件夾
File a=newFile(oldPath);
String[] file=a.list();
File temp=null;for (int i = 0; i < file.length; i++) {if(oldPath.endsWith(File.separator)){
temp=new File(oldPath+file[i]);
}else{
temp=new File(oldPath+File.separator+file[i]);
}if(temp.isFile()){
FileInputStream input= newFileInputStream(temp);
FileOutputStream output= new FileOutputStream(newPath + "/" +(temp.getName()).toString());byte[] b = new byte[1024 * 5];intlen;while ( (len = input.read(b)) != -1) {
output.write(b,0, len);
}
output.flush();
output.close();
input.close();
}if(temp.isDirectory()){//如果是子文件夾
copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);
}
}
}catch(Exception e) {
System.out.println("復(fù)制整個(gè)文件夾內(nèi)容操作出錯(cuò)");
e.printStackTrace();
}
}
總結(jié)
以上是生活随笔為你收集整理的java 文件拷贝文件怎么打开_java如何拷贝文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 晴天宝珠的BOOS咋打!
- 下一篇: 治输卵管堵塞做造影多少钱