我一直在嘗試通過查找和替換名稱中的子字符串來重命名給定文件夾中的文件和文件夾。另外,文件的名稱也包含在它們的內容中。我需要將其替換爲新名稱。替換文件中包含的文件名,並用java的新名稱重命名文件
例如:中的所有文件和文件夾名稱
更改 「XXX」 到 「KKK」 和也在文件內容:
原始文件名:0001_XXX_YYY_ZZZ.txt
新文件名:0001_KKK_YYY_ZZZ.txt
以下是我使用的代碼。
當我運行下面的代碼而不調用函數replaceText()時,它重命名文件和文件夾。但是,當我嘗試更改文件的文本並重命名文件和文件夾時;文件的內容被改變,但文件和文件夾的重命名失敗。
請幫忙。看完後
public class FindReplaceAnywhere {
public static void main(String[] args) {
String find = "XXX";
String replace = "KKK";
String baseLoc = "D:\\0001_XXX_YYY_ZZZ";
FindReplaceAnywhere obj = new FindReplaceAnywhere();
File baseLocObj = new File(baseLoc);
LinkedList<File> baseFolderList = new LinkedList<File>();
// Add base folder object to list
baseFolderList.add(baseLocObj);
// Get list of files in the folder
for(File file: baseLocObj.listFiles()) {
baseFolderList.add(file);
}
// Rename the files, folders & contents of files
obj.rename(baseFolderList, find, replace);
}
public void rename(LinkedList<File> fileList, String find, String replace) {
String tempStr = null;
int beginIndex = 0;
int endIndex = 0;
File tempFile;
System.out.println(">>> Batch Rename Process Begins >>>\n");
for(File aFile:fileList) {
// If Object is File, change the text also
if(aFile.isFile()) {
replaceText(aFile,find,replace);
}
}
for(File aFile: fileList) {
System.out.println("Processing>>>");
System.out.println(aFile.getPath());
if(aFile.getName().contains(find)) {
// Get the name of File object
beginIndex = aFile.getPath().length() - aFile.getName().length();
endIndex = aFile.getPath().length();
tempStr = aFile.getPath().substring(beginIndex, endIndex);
tempStr = tempStr.replace(find, replace);
}
else {
System.out.println("Error: Pattern not found\n");
continue;
}
tempFile = new File(aFile.getParentFile(),tempStr);
boolean success = aFile.renameTo(tempFile);
if(success) {
System.out.println("File Renamed To: "+tempFile.getName());
}
else {
System.out.println("Error: Rename Failed\nPossible Cause: File is open in another application");
}
System.out.println("");
}
}
/**
* Replace the text of file if it contains filename
*/
public void replaceText(File file, String find, String replace) {
String fullText = "";
String line = "";
String fileName = "";
String replaceName = "";
BufferedReader in;
BufferedWriter out;
// Read the file contents
try {
in = new BufferedReader(new FileReader(file));
while((line = in.readLine()) != null) {
fullText+=line+"\n";
}
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
// Replace the text of file
fileName = file.getName().substring(0, file.getName().indexOf("."));
replaceName = fileName.replace(find, replace);
fullText = fullText.replace(fileName, replaceName);
// Write the replaced text to file
try {
out = new BufferedWriter(new FileWriter(file));
out.write(fullText);
out.close();
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
}
}
什麼是*確切*您所面臨的問題?只是說它不起作用是不夠的。 – gparyani
你應該儘量減少代碼量,只提供相關部分,導致沒有人會像200行代碼一樣查看。另外指定你得到的錯誤和它發生的行。 – svz
請參閱http://sscce.org/以獲取指導。 @svz它不一定是一個例外;這可能只是代碼沒有達到預期目的。那麼, – gparyani