1

I need to extract String parts from each file in a folder(files) having the following extensions as png,jpg,jpeg,PNG,JPG,JPEG. The file naming convention is as below(These are 2 different files but the only thing they have in common is the TIMESTAMP which will be required to get the FILENAME:

AIRLINECODE_PARTNERCODE_INVOICENO_timestamp.FILETYPE FILENAME.FILETYPE_timestamp

Example file names:

ET_PRH_4_20170309140404.png

gosalyn.png_20170309140404

After reading the field from the first, I need to write each of the capital fields to the database (for eg AIRLINECODE, PARTNERCODE to columns in db). The following is the code I have written so far, so could you kindly guide me as how to proceed from here. Your help will be much appreciated. I only need help for the splitting and storing part. Thank you

import java.io.File;
import java.io.FilenameFilter;

public class Pathnames {
    
    public void readFilename() {
        // try-catch block to handle exceptions
        try {
            File f = new File("C:\\Users\\rsaeed\\Desktop\\files");

            FilenameFilter filter = new FilenameFilter() {
                @Override
                public boolean accept(File f, String name) {
                    return name.endsWith(".png") || name.endsWith(".PNG") || name.endsWith(".jpg") || name.endsWith(".JPG") || name.endsWith(".jpeg") || name.endsWith(".JPEG");
                }
            };

            // using a File class as an array
            File[] files = f.listFiles(filter);
            if(files != null) {
                for(File eachFile : files) {
                    String[] partsOfName = eachFile.split("_"); // this part is wrong. I am stuck here
                }
            }

            
            // Get the names of the files by using the .getName() method
            for (int i = 0; i < files.length; i++) {
                System.out.println(files[i].getName());
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
    
    
    public static void main(String args[]) {
        
        Pathnames p = new Pathnames();
        p.readFilename();
    }
}
6
  • What is the "specific" problem you are stuck on? Commented Apr 12, 2021 at 14:24
  • @OldProgrammer I am able to get the filenames from the folder but for each file I need to split it parts according to what I have described above and write it to database. Writing to db part will come later first I need to split and store the specific fields (the capitalized ones). Commented Apr 12, 2021 at 14:27
  • @OldProgrammer did you get it or do you think I need to explain further? Commented Apr 12, 2021 at 14:48
  • So you are interested in the capital words only? Can they occur multiple times in the string? Or are they always at the beginning? Commented Apr 12, 2021 at 15:22
  • 1
    Perhaps you could use a pattern to extract the parts, group 1 has the uppercase filename that you can split on _ and check group 2 and 3 for existence and get the timestamp^([A-Z]+(?:_[A-Z]+)*).*(?:_(\d+)\.(?:png|jpe?g|PNG|JPE?G)|\.(?:png|jpe?g|PNG|JPE?G)_(\d+))$ See regex101.com/r/l4zzFr/1 Commented Apr 12, 2021 at 15:43

1 Answer 1

1

You need to call split method on fileName, a String object, and not on File object.
While splitting use _ and . so that file extension will also get separated:

String fileName = "ET_PRH_4_20170309140404.png";
String[] parts = fileName.split("_|\\.");
System.out.println(Arrays.toString(parts));

Output:

[ET, PRH, 4, 20170309140404, png]

Now you can easily get parts from the array.
For more information on patterns refer Pattern class documentation.

For each file you can do it like this:

for(File eachFile : files) {
    String[] partsOfName = eachFile.file.getName().split("_|\\."); 
    final String timestamp = parts[3];
    //create a filter with `name.contains(timestamp)`
    //and execute f.listFiles again to get the related files.
}
Sign up to request clarification or add additional context in comments.

3 Comments

yes your solution seems fine but I need to do it for each file in the "files" folder and that is why I am trying to use the for loop to iterate through "eachFile" in "files". Can you offer a solution that can take this thing into aspect?
Also how can I compare the 2 files which have similar timestamp and use it to get the FILENAME? like anything which can be done within this code
Your filter is searching for only files with image extensions. Once you get the time stamp you'll have to search files with name containing the time stamp again. I've added the logic to the answer. Or modify your filter to name.contains("\\.png") And do time stamp search in the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.