【问题标题】:Referring files in a folder with similar name,editing , and then renaming them individually引用具有相似名称的文件夹中的文件,编辑,然后单独重命名它们
【发布时间】:2016-06-01 09:41:37
【问题描述】:

我有一组文件,例如“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”。 .etc 在一个文件夹中。我想为每个文件添加一些内容并将它们重命名为“f-1new.txt”、“g-2new.txt”。如何在 java 中引用它们,最好使用通配符并适当地重命名它们?

对于一个特定的文件,我使用 BufferedReader 读取其内容并使用 Printwriter 将修改后的内容写入新文件名。但是如果名称变化太大(但保持order) 像上面描述的那些?

我已经参考this,但它对如何获取数组中每个文件的文件名没有帮助(帖子中的第一个答案)..

【问题讨论】:

    标签: java file wildcard


    【解决方案1】:

    尝试以下方法:

    //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
    }
    

    【讨论】:

      【解决方案2】:

      这是一种解决方案。

      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();
      
              }
      
          }
      
      }
      }
      

      【讨论】:

      • 好吧,假设我只想读取文件夹中名称中有特殊模式的那些文件。我想我可能需要使用通配符。我应该怎么做?
      • 您可以像allFiles[l].getName().matches("f-\\d+\\.txt");一样使用matches()等字符串方法。例如,这匹配 f-x.txt 文件。
      • 你能告诉我这个“d+”是什么意思吗?
      • 我建议你看看正则表达式(regex)。 \d 是任何数字 [0-9],+ 是量词,表示一次或多次。 \d+ 表示一位或多位数字。 \ 是转义字符,因此需要使用另一个反斜杠对其进行转义。这就是为什么 \d+ 变成 \\d+
      猜你喜欢
      • 1970-01-01
      • 2011-01-27
      • 1970-01-01
      • 1970-01-01
      • 2022-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-14
      相关资源
      最近更新 更多