日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

复制文件,并重命名

發(fā)布時間:2024/7/19 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 复制文件,并重命名 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

總體思路: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é)

以上是生活随笔為你收集整理的复制文件,并重命名的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。