开发者

read files from directory and filter files from Java

The following codes goes through all directories and sub-directories and outputs just .java files;

import java.io.File;


public class DirectoryReader {
    private static String extension = "none";
    private static String fileName;

    public static void main(String[] args ){
        String dir = "C:/tmp";
        File aFile = new File(dir);
        ReadDirectory(aFile);
    }
    private static void ReadDirectory(File aFile) {
        File[] listOfFiles = aFile.listFiles();
        if (aFile.isDirectory()) {
             listOfFiles = aFile.listFiles();
            if(listOfFiles!=null) {
                for(int i=0; i < listOfFiles.length; i++  ) {
                    if (listOfFiles[i].isFile()) {

                        fileName = listOfFiles[i].toString();
                        int dotPos = fileName.lastIndexOf(".");
       开发者_如何学Go                 if (dotPos > 0) {
                            extension = fileName.substring(dotPos);
                        }                       
                        if (extension.equals(".java")) {
                        System.out.println("FILE:" + listOfFiles[i] );
                        }
                    }           
                    if(listOfFiles[i].isDirectory()) {
                        ReadDirectory(listOfFiles[i]);
                    }
                }
            }
        }
    }


}

Is this efficient? What could be done to increase the speed?

All ideas are welcome.


In Java 7, I would consider something like:

Files.walkFileTree(aFile.toPath(), new SimpleFileVisitor<Path>()
{
  public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
  {
    if(file.getFileName().endsWith(".java")
    {
      System.out.println("FILE:" + file );
    }
    return FileVisitResult.CONTINUE;
  }
});

This may not be faster, but I find it more readable.

EDIT: I removed the current Java example. listFiles is not recursive, so you can use it, but you would have to use your own recursion. You would also either need two listFiles calls (for Java files and directories), or you would need a FileFilter that matches both. In the latter case, you would have to check every file in the returned array to see which it is.


My comments:

  • Possible bug: extension is not reset. Think about what happens if you ecounter file1.java and thes file2 (without a .-character?)

    Suggestion: put

    if (extension.equals(".java"))
        System.out.println("FILE:" + listOfFiles[i] );
    

    inside the body of the if (dotPos > 0)

  • ReadDirectory is a method and should accourding to convention have small first letter: readDirectory

  • You could clean up the code a bit by using the for-each loop:

    for(File f : listOfFiles) {
        if (f.isFile()) {
            // ...
        }           
        if(f.isDirectory()) {
            readDirectory(f);
        }
    }
    


Use StringBuffer instead of System.out. Recursion is always slower albeit more concise.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜