要求:
(1)给定一个文件夹,文件夹中有大量视频文件
(2)给定一个Text,给定了文件夹中原文件名和新文件名的对应关系
(3)将文件夹中原文件名修改为新名称
Text中内容如下:
思路:
(1)text按行遍历,用HashMap存在原名+新名
(2)遍历HashMap找到对应的文件,修改文件名称即可
源码:
public class FileChangeName {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileChangeName a = new FileChangeName();
Map<String, String> map = a.readTxtFile("d:/Program Files/MyeclipseWorkSpace1/SF/src/com/wimb/A/change");//找到text文件
for (Map.Entry<String,String> entry : map.entrySet()) {
//根据map的建找到对应的视频文件
// File file = new File("d:/ChangName/"+entry.getKey()+".avi");
// file.renameTo()
a.renameFile("d:/ChangName/"+entry.getKey()+".avi", "d:/ChangName/"+entry.getValue()+".avi");
}
}
public Map<String,String> readTxtFile(String filePath) {
Map<String,String> filemaps = new HashMap<String,String>();
try {
String encoding = "GBK";
File file = new File(filePath);
if (file.isFile() && file.exists()) {
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
// System.out.println(“lineTxt=” + lineTxt);
String str = lineTxt;
String []arr = str.split(" ");
//System.out.println(arr[0]+"-----");
//System.out.println(arr[1]);
filemaps.put(arr[0], arr[1]);
}
read.close();
bufferedReader.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
return filemaps;
}
public void renameFile(String file, String toFile) {
File toBeRenamed = new File(file);
if (!toBeRenamed.exists() || toBeRenamed.isDirectory()) {
System.out.println("File does not exist: " + file);
return;
}
File newFile = new File(toFile);
if (toBeRenamed.renameTo(newFile)) {
System.out.println("File has been renamed.");
} else {
System.out.println("Error renmaing file");
}
}
}