當(dāng)前位置:
首頁 >
复制文件,并重命名
發(fā)布時間:2024/7/19
42
豆豆
總體思路:1. 先復(fù)制文件到指定目錄下;
? 2. 根據(jù)需要,修改文件名稱。
?
(1)復(fù)制文件 實現(xiàn)代碼:
例如:把“D:\照片”目錄下的文件復(fù)制到“D:\姓名”目錄下。
/*** 描述:復(fù)制文件 到 目標路徑
* @param srcPath
* @param targetPath
* @throws Exception
*/
public static void copyFile(String srcPath, String targetPath) throws Exception{
File srcFolder = new File(srcPath);
File tarFolder = new File(targetPath);
if(!tarFolder.exists()){
tarFolder.mkdirs();
}
File[] srcFiles = srcFolder.listFiles();
InputStream ins = null;
OutputStream ots = null;
for(File srcFile:srcFiles){
if(srcFile.exists()){
String fileName = srcFile.getName();
ins = new FileInputStream(srcFile);
ots = new FileOutputStream(targetPath+"\\"+fileName);
int reader = -1;
byte[] readByte = new byte[2048];
while((reader=ins.read(readByte))!=-1){
ots.write(readByte,0,reader);
}
}
}
if(ots!=null){
ots.close();
}
if(ins!=null){
ins.close();
}
}
(2)修改文件名稱:renameTo(File targetFile)函數(shù)。
轉(zhuǎn)載于:https://www.cnblogs.com/xiaonier/p/6042045.html
總結(jié)
- 上一篇: 写在开始之前
- 下一篇: jQuery基本过滤选择器