File list in java filter

Java: List Files in a Directory

A lot of applications handle files in some way and file manipulation is one of the core knowledges in any programming language.

In order to manipulate files, we need to know where they’re located. Having an overview of files in a directory is paramount if we want to accomplish this, especially if we can perform operations on them through iteration. There are several ways to do this in Java, which we’ll show throughout this article.

For the sake of simplicity, all examples will be written for the following file tree:

Programming |-- minimax.c |-- super_hack.py |-- TODO.txt `-- CodingMusic |-- Girl Talk - All Day.mp3 |-- Celldweller - Frozen.mp3 |-- Lim Taylor - Isn't It Wonderful.mp3 `-- Radiohead - Everything in Its Right Place.mp3 

File.list()

The simplest method to list the names of files and folders in a given directory, without traversing the sub-directories, is the helper method .list() , which returns an array of String s.

We do this by using the .list() method on a File instance:

public class Pathnames < public static void main(String[] args) < // Creates an array in which we will store the names of files and directories String[] pathnames; // Creates a new File instance by converting the given pathname string // into an abstract pathname File f = new File("D:/Programming"); // Populates the array with names of files and directories pathnames = f.list(); // For each pathname in the pathnames array for (String pathname : pathnames) < // Print the names of files and directories System.out.println(pathname); > > > 

Using a simple for-each loop, we iterate through the array and print out the String s.

CodingMusic minimax.c super_hack.py TODO.txt 

Using this approach, all of the items in the CodingMusic directory aren’t shown, and a downside to this approach is that we can’t really do anything to the files themselves. We’re just getting their names. It’s useful when we just want to take a look at the files at face-value.

FilenameFilter

Another thing we can do with the .list() method is to create a FilenameFilter to return only the files we want:

File f = new File("D:/Programming"); // This filter will only include files ending with .py FilenameFilter filter = new FilenameFilter() < @Override public boolean accept(File f, String name) < return name.endsWith(".py"); > >; // This is how to apply the filter pathnames = f.list(filter); 

Running this piece of code would yield:

File.listFiles()

Similar to the previous method, this one can be used to return the names of files and directories, but this time we get them as an array of File objects, which gives us the ability to directly manipulate them:

public class Pathnames < public static void main(String args[]) < // try-catch block to handle exceptions try < File f = new File("D:/Programming"); FilenameFilter filter = new FilenameFilter() < @Override public boolean accept(File f, String name) < // We want to find only .c files return name.endsWith(".c"); > >; // Note that this time we are using a File class as an array, // instead of String File[] files = f.listFiles(filter); // 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()); >> > 

Now, let’s traverse deeper into the file system using recursion and some more methods to use on the File object:

public class ListFilesRecursively < public void listFiles(String startDir) < File dir = new File(startDir); File[] files = dir.listFiles(); if (files != null && files.length > 0) < for (File file : files) < // Check if the file is a directory if (file.isDirectory()) < // We will not print the directory name, just use it as a new // starting point to list files from listDirectory(file.getAbsolutePath()); > else < // We can use .length() to get the file size System.out.println(file.getName() + " (size in bytes: " + file.length()+")"); > > > > public static void main(String[] args) < ListFilesRecursively test = new ListFilesRecursively(); String startDir = ("D:/Programming"); test.listFiles(startDir); > > 
Girl Talk - All Day.mp3 (size in bytes: 8017524) Celldweller - Frozen.mp3 (size in bytes: 12651325) Lim Taylor - Isn't It Wonderful.mp3 (size in bytes: 6352489) Radiohead - Everything in Its Right Place.mp3 (size in bytes: 170876098) minimax.c (size in bytes: 20662) super_hack.py (size in bytes: 114401) TODO.txt (size in bytes: 998) 

Files.walk()

In Java 8 and later we can use the java.nio.file.Files class to populate a Stream and use that to go through files and directories, and at the same time recursively walk all subdirectories.

Читайте также:  HTML Definition List

Note that in this example we will be using Lambda Expressions:

public class FilesWalk < public static void main(String[] args) < try (Stream walk = Files.walk(Paths.get("D:/Programming"))) < // We want to find only regular files List result = walk.filter(Files::isRegularFile) .map(x -> x.toString()).collect(Collectors.toList()); result.forEach(System.out::println); > catch (IOException e) < e.printStackTrace(); >> > 

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

Here, we’ve populated a Stream using the .walk() method, passing a Paths argument. The Paths class consists of static methods that return a Path based on a String URI — and using a Path , we can locate the file easily.

The Path , Paths , Files , and many other classes belong to the java.nio package, which was introduced in Java 7 as a more modern way to represent files in a non-blocking way.

Then, using the Collections Framework, a list is generated.

Running this piece of code will yield:

D:\Programming\Coding Music\Radiohead - Everything in Its Right Place.mp3 D:\Programming\Coding Music\Lim Taylor - Isn't It Wonderful.mp3 D:\Programming\Coding Music\Celldweller - Frozen.mp3 D:\Programming\Coding Music\Girl Talk - All Day.mp3 D:\Programming\minimax.c D:\Programming\super_hack.py D:\Programming\TODO.txt 

Conclusion

Handling files in some way is a core task for most programming languages, and this includes the ability to list and find files in the file system. In order to manipulate files, we need to know where they’re located. Having an overview of files in a directory is paramount if we want to accomplish this, especially if we can perform operations on them through iteration.

In this article we showed a number of different ways in Java to list files on the file system, using both linear and recursive approaches.

Читайте также:  >

Источник

Java.io.File.list() Method

The java.io.File.list(FilenameFilter filter) returns the array of files and directories in the directory defined by this abstract path name that satisfy the given filter. All names are accepted if the given filter is null.

Declaration

Following is the declaration for java.io.File.list(FilenameFilter filter) method −

public String[] list(FilenameFilter filter)

Parameters

Return Value

The method returns the array of files and directories in the directory defined by this abstract path name that satisfy the given filter.

Exception

SecurityException − If a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies read access to the file

Example

The following example shows the usage of java.io.File.list(FilenameFilter filter) method.

package com.tutorialspoint; import java.io.File; import java.io.FilenameFilter; public class FileDemo implements FilenameFilter < String str; // constructor takes string argument public FileDemo(String ext) < str = "."+ext; >// main method public static void main(String[] args) < String[] paths; try < // create new file f = new File("c:/test"); // create new filter FilenameFilter filter = new FileDemo("txt"); // array of files and directory paths = f.list(filter); // for each name in the path array for(String path:paths) < // prints filename and directory name System.out.println(path); >> catch(Exception e) < // if any error occurs e.printStackTrace(); >> @Override public boolean accept(File dir, String name) < return name.endsWith(str); >>

Let us compile and run the above program, this will produce the following result −

Источник

Java File listFiles(FileFilter filter) method with examples

The listFiles(FileFilter filter) method returns an array of File objects that represent the files and directories that satisfy specified FileFilter .

2. Method signature

public File[] listFiles(FileFilter filter) 

Parameters:

Returns

  • File [] — an array of File objects that represent files and directories that satisfy the specified filter.

Throws

3. Examples

3.1. Code snippet that prints only files (!file.isDirectory()) with the .txt extension (file.getName().endsWith(«.txt»))

package com.frontbackend.java.io; import java.io.File; import java.io.FileFilter; import java.io.FilenameFilter; import java.util.Arrays; public class FrontBackend < public static void main(String args[]) < try < File tmp = new File("/tmp/ttt"); FileFilter fileFilter = file ->!file.isDirectory() && file.getName() .endsWith(".txt"); File[] list = tmp.listFiles(fileFilter); if (list != null) < Arrays.stream(list) .forEach(file ->< System.out.println(file.getPath() + " " + (file.isDirectory() ? "dir" : "file")); >); > > catch (Exception e) < e.printStackTrace(); >> > 
/tmp/ttt/dest.txt file /tmp/ttt/test.txt file 

4. Conclusion

In this article, we presented the listFiles(FileFilter filter) method that could be used to filter files and directories from the specified path in the filesystem using FileFilter object.

Читайте также:  Html tags на русском

Источник

Java FilenameFilter to Find Files Matching Pattern

Many times we need to traverse and find all files with a certain name pattern to do some operations on those files, for example deleting those files. This is more often required when we want to delete all .log or .tmp files from the server after a certain time using the application (if such a requirement exists).

In Java, we can use FilenameFilter class. It tests if a specified file should be included in a file list. To use FilenameFilter, override the accept(dir, name) method that contains the logic to check if the file has to be included in the filtered list.

From Java 8 onwards, FileNameFilter is a functional interface. The classes that implement this interface are used to filter filenames. It has a single method accept() that takes two parameters:

The given LogFilterFilter class can be used to filter all log files from a list of files.

public class LogFilterFilter implements FilenameFilter < @Override public boolean accept(File dir, String fileName) < return fileName.endsWith(".log"); >>

2. How to use FilenameFilter

The best way to use the FileNameFilter is to pass it to one of the following methods in java.io.File class where File represents a directory location:

  • String[] list(filter) : returns an array of strings naming the files and directories in the target directory.
  • File[] listFiles(filter) : returns an array of files and directories in the target directory.

3. FilenameFilter Examples

Let us look at a few examples to understand how we can use the FilenameFilter class.

Example 1: Java program to use FilenameFilter to find all log files

In this example, we will use FilenameFilter instance to list out all «.log» files in folder «c:/temp» . We will also delete all these log files.

String targetDirectory = "c:\\temp"; File dir = new File(targetDirectory); //Find out all log files String[] logFiles = dir.list(new LogFilterFilter()); //If no log file found; no need to go further if (logFiles.length == 0) return; //This code will delete all log files one by one for (String fileName : logFiles)

Example 2: Creating FilenameFilter using Lambda Expression

Since FileNameFilter is a functional interface, we can reduce and create it using a lambda expression.

FilenameFilter logFileFilter = (d, s) -> < return s.toLowerCase().endsWith(".log"); >; String[] logFiles = dir.list(logFileFilter);

Example 3: Creating FilenameFilter containing Regular Expressions

This Java program filters all files based on file names matching a regular expression. For example, we want to list all the files that do not contain a number in their names.

FilenameFilter filter = (d, s) -> < return s.matches("[a-zA-z]+\\.[a-z]+"); >; String[] filteredFiles = dir.list(filter);

In this Java tutorial, we learned to use the FilenameFilter to traverse a directory and search all files with names matching a specified pattern.

Источник

Оцените статью