0

I am trying to read files from unix file system with a pattern format in a java program.

The file name pattern is like this:

"XYZ"+"abcd1234"+MMddyyyyHHmmss

For example: XYZabc123403222012101329

The middle pattern of "abcd1234" is not fixed but changes, so it will be alphanumeric and total character length of 7.

How can I read the file with such pattern? The file can contain text or serialized object.

String path="/tmp/somedir";

final File folder = new File("/tmp/somedir");
List<String> fileNames = new ArrayList<String>();

    try {
        for (final File fileEntry : folder.listFiles()) {
            if (fileEntry.isDirectory()) {
                listFilesForFolder(fileEntry);
            } else {
                //System.out.println(fileEntry.getName());
                fileNames.add(fileEntry.getName());
            }
        }

        for(String str: fileNames) {
            FileInputStream fileIn = new FileInputStream(path+"/"+str);
        }

    } catch(FileNotFoundException e) {
        e.printStackTrace();
    } catch(Exception e1) {
        e1.printStackTrace();
    }

Thank You.

2
  • 3
    I think you want to look at making your own File filter, see: docs.oracle.com/javase/1.4.2/docs/api/java/io/… Commented Aug 22, 2012 at 18:55
  • You have to search the directory for a file which matches your requirements. Commented Aug 22, 2012 at 19:00

3 Answers 3

2

I think you can use reguler expression for filtering files. Here is the class for doc for apache commons http://commons.apache.org/io/api-release/org/apache/commons/io/filefilter/RegexFileFilter.html and here is an old question about it. Java regexp for file filtering

Sign up to request clarification or add additional context in comments.

Comments

0

You may be able to use a regular expression. I'm doing the exact same thing in C# right now. I have files that come from clients that have a common prefix, a date in the middle, and a common file extension.

Assuming that the abc1234 is always seven characters long, you could use the following regular expression pattern.

XYZ.{7}\d{14}

The .{7} means that any character can appear there up to seven times. The \d{14} means that it will count up to 14 digits.

1 Comment

You are welcome and thank you for thanking me. A lot of people certainly do not. Good luck with your project.
0

I recommend the this site, you'll find out how to get all the files from your directory.

After this, in the method : accept apply a regular expression on its parameter : name.

For regular expression, look toward this link.

Comments

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.