-1

For example i have a directory contains the below files:

  1. test.log
  2. test.html
  3. test.txt

I would like to know how I can make my java program pick and read only the files with .log extension.

Because the name of the file is always changing and I want to trace and read the files that have .log extension.

Any idea ?

Shadow
  • 3,926
  • 5
  • 20
  • 41
L29101987
  • 25
  • 2
  • 7
  • Look [here](http://stackoverflow.com/questions/5751335/using-file-listfiles-with-filenameextensionfilter) this should help you. – Jens Oct 28 '14 at 07:11
  • http://docs.oracle.com/javase/7/docs/api/java/io/File.html#listFiles%28java.io.FileFilter%29, http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#endsWith%28java.lang.String%29 – JB Nizet Oct 28 '14 at 07:16

5 Answers5

0

Try this

public boolean accept(File file) {
      return file.getName().toLowerCase().endsWidth(".log");
    }
  • 2
    To compare stings you have to use `equals` not `==` – Jens Oct 28 '14 at 07:12
  • So, what if the file is named foo.log.txt? BTW, split(".") doesn't do what you think it does. – JB Nizet Oct 28 '14 at 07:15
  • You should remove the first part, which is plain wrong. The body of the accept method should be rewritten as `return file.getName().toLowerCase().endsWidth(".log");` – JB Nizet Oct 28 '14 at 07:28
  • 1
    @Suganthan What @JBNizet is trying to tell: The [`split`](http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-) method works on [regular expressions](http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html). So the first part is wrong _and_ unnecessary. The second part is written as `if () return true; /*else*/ return false;`. This can and should be written as a simple `return ;`. This is more readable. – Seelenvirtuose Oct 28 '14 at 07:34
0

Try following code:

File file=new File("path.log");
String name=file.toString();
String tok[]=name.split("\\.");
if(tok[tok.length-1].equals("log")){
    //compute
}

Above is the core logic of checking the extension for being log. Below code is to iterate a directory and look for a file with log extension.

File file=new File("C:/Temp/");
String name,tok[];
for(File temp:file.listFiles()){
    name=temp.toString();
    tok=name.split("\\.");
    if(tok[tok.length-1].equals("log")){
        //compute
    }
}

For more on File visit this link.

Darshan Lila
  • 5,772
  • 2
  • 24
  • 34
0

You can use a simple regex to match the extension of the file:

if(file.toString().matches(".*\\.log"))
{
    //Process the file
}
Shadow
  • 3,926
  • 5
  • 20
  • 41
0

I use Apache Commons IO (WildcardFileFilter)

File dir = new File("/path/to/directory");
FileFilter fileFilter = new WildcardFileFilter("*.log");

File[] files = dir.listFiles(fileFilter);
for (int i = 0; i < files.length; i++)
   System.out.println(files[i]); // do something
Mitesh Pathak
  • 1,306
  • 10
  • 14
0

See following snippet

    Path path = Paths.get("resources/");
    try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(path, "*.{zip,txt}")) {
        for (Path entry : dirStream) {
            System.out.printf("%-5s: %s%n", "entry", entry.getFileName());
        }
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }

For more details have a look into this full example: https://github.com/jugsaar/jugsaar-meeting-9/blob/master/talks/java-nio2/src/main/java/de/jugsaar/meeting9/nio2/directorystream/DirectoryStreamGlobbingPatternDemo.java

SubOptimal
  • 22,518
  • 3
  • 53
  • 69