0

I have used the nio file package to read all the the files of a given extension from a given folder/ sub-folder in Java 1.7! However, my code does not work in Java 6 environment. There are no JARS available for the nio package.

Is there any piece of code to replace the use of these packages? I'm sharing my code here.

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;

public class FileFinder extends SimpleFileVisitor<Path> {
    private PathMatcher matcher;
    public ArrayList<Path> foundPaths = new ArrayList<Path>();

    public FileFinder(String pattern) {
        matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        Path name = file.getFileName();

        if (matcher.matches(name)) {
            foundPaths.add(file);
        }

        return FileVisitResult.CONTINUE;
    }
}
  • 5
    Why do you need to revert back to Java 6? It is no longer supported, and as you found, is missing a lot of functionality that is considered standard today. – Jim Garrison Jul 13 '16 at 06:49
  • use `java.io.File` – Jens Jul 13 '16 at 06:50
  • Unfortunately, client side still works on Java 6. – Chinese Monger Jul 13 '16 at 06:50
  • are you trying to code for some legacy application , if yes then change compiler ,jre version to 1.6 and then develop, – bananas Jul 13 '16 at 06:50
  • @AsteriskNinja how would that help here, exactly? APIs don't exist there – eis Jul 13 '16 at 06:51
  • @AsteriskNinja And how should this help? – Jens Jul 13 '16 at 06:51
  • It is possible to do this using the APIs that existed in Java 6, just a lot more work. You have to write the recursive tree walking code yourself. Explaining how to do that is probably too broad for SO. – Jim Garrison Jul 13 '16 at 06:52
  • actyally I had same problem , developed code on `jdk8` does not supported on `jdk6` , then it took some research and lots of time but got it solved ... so give it a try – bananas Jul 13 '16 at 06:53
  • 1
    Years ago I posted this method: http://stackoverflow.com/questions/2056221/recursively-list-files-in-java/2056326#2056326 – stacker Jul 13 '16 at 06:53
  • why don't you try with `java.io.*` . Hope you will be able to do this using that. – Kapila Ranasinghe Jul 13 '16 at 10:37

0 Answers0