-1

I have a set of files like "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 in a folder.I want to add a few things to each of them and rename them like "f-1new.txt" , "g-2new.txt". How can I refer them in java preferably using wildcard and rename them suitably?

For a particlar file, I use BufferedReader to read its contents and Printwriter to write modified contents to a new file name.. But how can I read contents from all files, (iteratively) if the names varies too much(yet maintain order) like the ones described above?

I have refrred to this but it didnt help me with how to get the file names of each file in the array(the first answer in the post)..

Community
  • 1
  • 1
Qwerty
  • 141
  • 10

2 Answers2

1

Try the following:

//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
}
Rambler
  • 4,994
  • 2
  • 20
  • 27
0

Here is one solution.

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

        }

    }

}
}
MAZDAK
  • 573
  • 1
  • 4
  • 16
  • Well suppose I wanted to read only those files in my folder which had a special pattern in their name.. I think I may need to use wildcards then.. How am I supposed to do so? – Qwerty Jun 01 '16 at 10:55
  • You can use String methods such as `matches()` as in `allFiles[l].getName().matches("f-\\d+\\.txt");`. This for instance matches f-x.txt files. – MAZDAK Jun 01 '16 at 11:36
  • Can you tell me what this 'd+' means? – Qwerty Jun 01 '16 at 13:39
  • I recommend you take a look at regular expressions (regex). \d is any digit [0-9], and + is a quantifier, meaning one or more times. \d+ means one or more digits. \ is the escape character so it needs to be escaped using another backslash. That's why \d+ becomes \\d+ – MAZDAK Jun 01 '16 at 13:50