Gooner Coder
Gooner Coder

Reputation: 25

Parsing filename to extract String parts in Java

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

Upvotes: 1

Views: 731

Answers (1)

the Hutt
the Hutt

Reputation: 18408

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

Upvotes: 1

Related Questions