引用具有相似名称的文件夹中的文件,编辑,然后分别重命名它们
Referring files in a folder with similar name,editing , and then renaming them individually
我有一组文件,例如 "f-1.txt"、"f-2.txt"、.....、"f-30.txt"、"g-1.txt"、"g-2.txt"、.. ... , "g-23.txt","h-1.txt" , "h-2.txt", ..... , "h-35.txt"..等 folder.I 想添加一些东西他们每个人都重命名为 "f-1new.txt" , "g-2new.txt"。我如何在 java 中引用它们,最好使用通配符并适当地重命名它们?
对于特定文件,我使用 BufferedReader 读取其内容,使用 Printwriter 将修改后的内容写入新文件名。但是如果名称变化太大,我如何从所有文件中读取内容(迭代)(还维持秩序)像上面描述的那样?
我已经参考 this 但它没有帮助我如何获取数组中每个文件的文件名(post 中的第一个答案)..
尝试以下操作:
//This method will return files with matching pattern in the specified directory
public File[] getMatchingFiles(String yourDirectoryWithFiles){
File directoryWithFiles= new File(yourDirectoryWithFiles);
return directoryWithFiles.listFiles(new FilenameFilter() {
public boolean accept(File dir, String filename)
{ //Make this dynamic with passing the pattern as an argument
return filename.endsWith("f.*txt");
}
} );
}
//Iterate over the files and rename them
public void iterateFiles(String yourDirectoryWithFiles){
File[] fileList=getMatchingFiles(yourDirectoryWithFiles);
for(File oldFile:fileList){
boolean success=createNewFile(oldFile);
//Case 1 :Deleting the old file if file creation was successful
if(success)
oldFile.delete();
//If using Case 2: return the newFileObject and call: oldFile.renameTo(newFile);
}
}
public boolean createNewFile(File oldFile){
//Case 1: create a new file object here and perform your name changing operations
//Case 2: If you don't want to create another file , write to the existing file
//but you would still need to create an file object to perform rename operation
}
这是一种解决方案。
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException {
String PATH_2_FOLDER = "path_2_folder";
//listing all files in the desired folder
File myDirectory = new File(PATH_2_FOLDER);
File[] allFiles = myDirectory.listFiles();
System.out.println(allFiles.length);
for (int l = 0; l < allFiles.length; l++) {
if (allFiles[l].getName().endsWith(".txt")) {
//read the input file
String thisPathIn = PATH_2_FOLDER+allFiles[l].getName();
BufferedReader thisBR = new BufferedReader(new FileReader(thisPathIn));
//create the output file
String newName = allFiles[l].getName().replace(".txt", "").concat(".new.txt");
String thisPathOut = PATH_2_FOLDER+newName;
BufferedWriter thisBW = new BufferedWriter(new FileWriter(thisPathOut));
//read the contents of the inputfile
String s = "";
while((s = thisBR.readLine()) != null){
//process the content
//...
//create new content
thisBW.write("new_content\n");
}
thisBW.flush();
thisBW.close();
}
}
}
}
我有一组文件,例如 "f-1.txt"、"f-2.txt"、.....、"f-30.txt"、"g-1.txt"、"g-2.txt"、.. ... , "g-23.txt","h-1.txt" , "h-2.txt", ..... , "h-35.txt"..等 folder.I 想添加一些东西他们每个人都重命名为 "f-1new.txt" , "g-2new.txt"。我如何在 java 中引用它们,最好使用通配符并适当地重命名它们?
对于特定文件,我使用 BufferedReader 读取其内容,使用 Printwriter 将修改后的内容写入新文件名。但是如果名称变化太大,我如何从所有文件中读取内容(迭代)(还维持秩序)像上面描述的那样?
我已经参考 this 但它没有帮助我如何获取数组中每个文件的文件名(post 中的第一个答案)..
尝试以下操作:
//This method will return files with matching pattern in the specified directory
public File[] getMatchingFiles(String yourDirectoryWithFiles){
File directoryWithFiles= new File(yourDirectoryWithFiles);
return directoryWithFiles.listFiles(new FilenameFilter() {
public boolean accept(File dir, String filename)
{ //Make this dynamic with passing the pattern as an argument
return filename.endsWith("f.*txt");
}
} );
}
//Iterate over the files and rename them
public void iterateFiles(String yourDirectoryWithFiles){
File[] fileList=getMatchingFiles(yourDirectoryWithFiles);
for(File oldFile:fileList){
boolean success=createNewFile(oldFile);
//Case 1 :Deleting the old file if file creation was successful
if(success)
oldFile.delete();
//If using Case 2: return the newFileObject and call: oldFile.renameTo(newFile);
}
}
public boolean createNewFile(File oldFile){
//Case 1: create a new file object here and perform your name changing operations
//Case 2: If you don't want to create another file , write to the existing file
//but you would still need to create an file object to perform rename operation
}
这是一种解决方案。
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException {
String PATH_2_FOLDER = "path_2_folder";
//listing all files in the desired folder
File myDirectory = new File(PATH_2_FOLDER);
File[] allFiles = myDirectory.listFiles();
System.out.println(allFiles.length);
for (int l = 0; l < allFiles.length; l++) {
if (allFiles[l].getName().endsWith(".txt")) {
//read the input file
String thisPathIn = PATH_2_FOLDER+allFiles[l].getName();
BufferedReader thisBR = new BufferedReader(new FileReader(thisPathIn));
//create the output file
String newName = allFiles[l].getName().replace(".txt", "").concat(".new.txt");
String thisPathOut = PATH_2_FOLDER+newName;
BufferedWriter thisBW = new BufferedWriter(new FileWriter(thisPathOut));
//read the contents of the inputfile
String s = "";
while((s = thisBR.readLine()) != null){
//process the content
//...
//create new content
thisBW.write("new_content\n");
}
thisBW.flush();
thisBW.close();
}
}
}
}