How to Scan a Folder in Java

How to scan a folder in Java?

Not sure how you want to represent the tree? Anyway here's an example which scans the entire subtree using recursion. Files and directories are treated alike. Note that File.listFiles() returns null for non-directories.

public static void main(String[] args) {
Collection<File> all = new ArrayList<File>();
addTree(new File("."), all);
System.out.println(all);
}

static void addTree(File file, Collection<File> all) {
File[] children = file.listFiles();
if (children != null) {
for (File child : children) {
all.add(child);
addTree(child, all);
}
}
}

Java 7 offers a couple of improvements. For example, DirectoryStream provides one result at a time - the caller no longer has to wait for all I/O operations to complete before acting. This allows incremental GUI updates, early cancellation, etc.

static void addTree(Path directory, Collection<Path> all)
throws IOException {
try (DirectoryStream<Path> ds = Files.newDirectoryStream(directory)) {
for (Path child : ds) {
all.add(child);
if (Files.isDirectory(child)) {
addTree(child, all);
}
}
}
}

Note that the dreaded null return value has been replaced by IOException.

Java 7 also offers a tree walker:

static void addTree(Path directory, final Collection<Path> all)
throws IOException {
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
all.add(file);
return FileVisitResult.CONTINUE;
}
});
}

How to scan a folder for files and their locations in java

Peek around in the java.io.File API. There are at least three methods which may be of use:

  • listFiles()
  • isDirectory()
  • isFile()

How to read all files in a folder from Java?

public void listFilesForFolder(final File folder) {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
System.out.println(fileEntry.getName());
}
}
}

final File folder = new File("/home/you/Desktop");
listFilesForFolder(folder);

Files.walk API is available from Java 8.

try (Stream<Path> paths = Files.walk(Paths.get("/home/you/Desktop"))) {
paths
.filter(Files::isRegularFile)
.forEach(System.out::println);
}

The example uses try-with-resources pattern recommended in API guide. It ensures that no matter circumstances the stream will be closed.

How to scan a file in a different directory in java?

By using an absolute path instead of a relative.

File file = new File("C:\\Data\\DataPacket99\\data.txt");

Then you can write code that accesses that file object, using a InputStream or similar.

How to scan a specific directory for files with a specific extension?

Old school very readable file name filter:

    File[] jsonFiles=new File("sourcePath").listFiles(new FilenameFilter() {
@Override
public boolean accept(File arg0, String name) {
return name.endsWith(".json");
}
});

Or if you want to use lambda stuff:
see java 8 lambda expression for FilenameFilter

How to scan an image folder and add them to an image array

Okay here is what I created:

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

public class ImageScan {

private List<Image> images = new ArrayList<Image>();

public void loadImages() {

List<Image> imageArray = new ArrayList<Image>();

File file = new File("data/images");

File[] imageFiles = file.listFiles(); // This gets all of the files inside
'file', if 'file' is a folder
for (File f : imageFiles) {
try {
BufferedImage image = ImageIO.read(f);
imageArray.add(image);
} catch (Exception e) {
// This makes sure only the images in the folder are used, not any
file.
}
}

this.setImages(imageArray);
}

public void setImages(List<Image> imageArray) {
images = imageArray;
}

public List<Image> getImages() {
return images;
}

}

Scan a file folder in android for file paths

You could use

List<String> paths = new ArrayList<String>();
File directory = new File("/mnt/sdcard/folder");

File[] files = directory.listFiles();

for (int i = 0; i < files.length; ++i) {
paths.add(files[i].getAbsolutePath());
}

See listFiles() variants in File (one empty, one FileFilter and one FilenameFilter).



Related Topics



Leave a reply



Submit