How to read file passing part of the name in Java

Asked

Viewed 1,689 times

7

Example.:

The name of my file would be 001nome.txt, this file will have a varying action on the name depending on the entity, ie one time it may be 001nome.txt another time it may be 999nome.txt.

Doubt.:

How can I move to Java to open this file independent of this variation in the name?

Observing: In the php I do so *nome.txt Then he returns the file that has the end nome.txt regardless of the beginning of your name.

3 answers

5

Using the Java API

Create an object File pointing to the directory and itere over the files checks if they have the desired pattern.

Iteration implementation can be done with the method File.listFiles(), whose parameter is a filter (implementation of FileFilter) and the return is an array of files (File[]).

To check the pattern in the file name, you can use a regular expression and the method matches() class String.

Take an example:

File pasta = new File(".");
File[] arquivos = pasta.listFiles(new FilenameFilter() {
    @Override
    public boolean accept(File file, String nome) {
        return nome.matches("\\d+nome.txt");
    }
});
for (File file : arquivos) {
    System.out.println(file.getName());
}

Using Apache Commons IO

However, if you can use a library like Apache Commons IO, it is possible to simplify using the RegexFileFilter or the WildcardFileFilter. See the examples:

File[] arquivos = pasta.listFiles(new RegexFileFilter(""\\d+nome.txt""));

Or:

File[] arquivos = pasta.listFiles(new WildcardFileFilter("*nome.txt"));

Using Google Guava

Another example with Google Guava:

File pasta = new File(".");
Iterable<File> iterable = Files
        .fileTreeTraverser()
        .breadthFirstTraversal(pasta)
        .filter(new Predicate<File>() {
            @Override
            public boolean apply(File input) {
                return input.getName().matches("\\d+nome.txt");
            }
        });
for (File f : iterable) {
    System.out.println(f.getAbsolutePath());
}

Although this is quite verbose, it allows iterating in subdirectories as well.

2

This method solved a similar problem I had.

FileFilter filter = new FileFilter() {  
    public boolean accept(File file) {  
        return file.getName().endsWith("name.txt");  
    }  
};

File dir = new File("/caminho/do/seu/diretorio");  
File[] files = dir.listFiles(filter);  

The .endsWith("name.txt"); can solve part of your problem, since it takes all files that end with the specified String.

1

Use an expression next to this. This expression allows something like 20140512_0000_xxxxxxx.xml.gz being yyyymmdd_hhmm_client.xml.gz

Pass this expression to the getFilter() then use the getListFiles()

This Regexfilefilter class is the project apache Commons io

"^[\\d]{8}_[\\d]{4}_[\\w]+\\.xml\\.gz$"

 private List<File> getListFiles() {
    List<File> list = Arrays.asList(this.sourceDirectory.listFiles(getFilter()));
    Collections.sort(list);
    return list;
  }    

  private FileFilter getFilter() {
    return new RegexFileFilter(FILE_FILTER);
  }
  • The class FileFilter is from Java, but the RegexFileFilter is actually from Commons IO.

  • 1

    Here in the project I am we prefer to use Commons IO for this facility and others also... we use a lot of Strinutils, Enumutils, Tostringutils... e por ae vai... hehehehe

Browser other questions tagged

You are not signed in. Login or sign up in order to post.