java?移動文件,并修改名稱方式
更新時間:2021年12月20日 11:35:56 作者:yfx000
這篇文章主要介紹了java?移動文件,并修改名稱方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
java 移動文件,并修改名稱
從source文件夾剪切1.txt,移動到target文件夾,并重命名為2.txt
//從source文件夾剪切1.txt,移動到target文件夾,并重命名為2.txt
File startFile=new File("D:\\source\\1.txt");
File endFile=new File("D:\\target\\2.txt");
if (startFile.renameTo(endFile)) {
System.out.println("文件移動成功!目標(biāo)路徑:{"+endFile.getAbsolutePath()+"}");
} else {
System.out.println("文件移動失敗!起始路徑:{"+startFile.getAbsolutePath()+"}");
}
復(fù)制單級文件夾中指定的文件并修改名稱
* 需求:復(fù)制指定目錄下的指定文件,并修改后綴名。 * 指定的文件是:.java文件。 * 指定的后綴名是:.jad * 指定的目錄是:jad * * 數(shù)據(jù)源:e:\\java\\A.java * 目的地:e:\\jad\\A.jad * * 分析: * A: 封裝目錄 * B: 獲取該目錄下的java文件的File數(shù)組 * C: 遍歷該File數(shù)組,得到每一個File對象 * D: 把該File進(jìn)行復(fù)制 * E: 在目的地目錄下改名
package cn.itcast_04;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
/*
* 需求:復(fù)制指定目錄下的指定文件,并修改后綴名。
* 指定的文件是:.java文件。
* 指定的后綴名是:.jad
* 指定的目錄是:jad
*
* 數(shù)據(jù)源:e:\\java\\A.java
* 目的地:e:\\jad\\A.jad
*
* 分析:
* A:封裝目錄
* B:獲取該目錄下的java文件的File數(shù)組
* C:遍歷該File數(shù)組,得到每一個File對象
* D:把該File進(jìn)行復(fù)制
* E:在目的地目錄下改名
*/
public class CopyFolderDemo {
public static void main(String[] args) throws IOException {
// 封裝目錄
File srcFolder = new File("e:\\java");
// 封裝目的地
File destFolder = new File("e:\\jad");
// 如果目的地目錄不存在,就創(chuàng)建
if (!destFolder.exists()) {
destFolder.mkdir();
}
// 獲取該目錄下的java文件的File數(shù)組
File[] fileArray = srcFolder.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return new File(dir, name).isFile() && name.endsWith(".java");
}
});
// 遍歷該File數(shù)組,得到每一個File對象
for (File file : fileArray) {
// System.out.println(file);
// 數(shù)據(jù)源:e:\java\DataTypeDemo.java
// 目的地:e:\\jad\DataTypeDemo.java
String name = file.getName();
File newFile = new File(destFolder, name);
copyFile(file, newFile);
}
// 在目的地目錄下改名
File[] destFileArray = destFolder.listFiles();
for (File destFile : destFileArray) {
// System.out.println(destFile);
// e:\jad\DataTypeDemo.java
// e:\\jad\\DataTypeDemo.jad
String name =destFile.getName(); //DataTypeDemo.java
String newName = name.replace(".java", ".jad");//DataTypeDemo.jad
File newFile = new File(destFolder,newName);
destFile.renameTo(newFile);
}
}
private static void copyFile(File file, File newFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java并發(fā)編程synchronized底層實現(xiàn)原理
這篇文章主要介紹了java并發(fā)編程synchronized底層實現(xiàn)原理2022-02-02
maven多個plugin相同phase的執(zhí)行順序
這篇文章主要介紹了maven多個plugin相同phase的執(zhí)行順序,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
Spring MVC 文件、cookies的接收 與REST響應(yīng)詳
在SpringMVC中,使用@RequestPart注解可接收文件并處理多部分請求,同時可以通過@CookieValue和HttpServletResponse來獲取和設(shè)置Cookies,本文介紹Spring MVC 文件、cookies的接收 與REST響應(yīng),感興趣的朋友跟隨小編一起看看吧2024-09-09

