0

I'm working on an application that will read in SystemOut.Log files and process them. Sometimes archived files may be named slightly differently, such as SystemOut_10:20_09/07/2021-10:45_09/07/2021.Log. It's always of the form SystemOut(Some more text here).Log.

I had a little read up and stumbled across wildcards and came to the conclusion that if I were to pass SystemOut*.Log into my application as the filename it would work. But I was wrong.

I originally get my filename through a properties file like so.

fileName=prop.getProperty("fileName");

I then just tried to concatenate *.Log on the end.

fileName=fileName+"*.Log";

When I print out fileName it is "SystemOut*.Log" but when I pass in this filename to my method that reads files it doesn't work as no file is found with that name.

Am I making an error in the code or have I just misunderstood how wildcards work? Thanks

  • It's the same question to this : https://stackoverflow.com/questions/794381/how-to-find-files-that-match-a-wildcard-string-in-java – Yiao SUN Jul 09 '21 at 10:02

1 Answers1

1

Try FileUtils from Apache commons-io (listFiles and iterateFiles methods): The code you need is

File dir = new File(".");
FileFilter fileFilter = new WildcardFileFilter("*.Log");
File[] files = dir.listFiles(fileFilter);
for (int i = 0; i < files.length; i++) {
   System.out.println(files[i]);
}
Yiao SUN
  • 908
  • 1
  • 8
  • 26
  • Ohh perfect thank you I didn't realize you had to take these extra steps. I mistakenly assumed you could just pass it into the filename. I'll install Apache now and get this sorted thank you – Connor Gill Jul 09 '21 at 10:11
  • `*.x` is known as *globbing* and is a feature of shells and thus will only work there. The solution proposed is the sort of thing you need, but there's no need for a 3rd party api to do it. – g00se Jul 09 '21 at 10:26
  • @g00se just out of interest then how would you do it using built in API? – Connor Gill Jul 09 '21 at 11:15
  • 1
    Strangely there are *two* classes for that in the api: `FileFilter` and `FilenameFilter` – g00se Jul 09 '21 at 11:37