簡體   English   中英

Java:解析目錄和子目錄以檢查特定類型的文件

[英]Java : Parsing directory and sub-directory to check for files of a particular type

說明:我正在嘗試解析我的主目錄以查找所有類型為“ .jpg”的文件,並且我的代碼能夠返回所有需要的文件。 例如“ C:\\ Ravi \\ Sources”,在此目錄中,我混合了.xml,.jpg,.gif文件,現在我在此目錄中也有子文件夾,但我不知道如何修改我的代碼以進行檢查以及子目錄。

這里需要專業知識幫助:

代碼段:

enter code here




import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import java.io.PrintStream;


public class Subdirectory {

    static File f = new File("C:\\Users\\kasharma\\Desktop\\Travelocity R8.3_8.3.0.apk\\res");// File f will represent the folder....

static String[] extensions = new String[]{"png", "jpg", "gif" };  // Declaring array of supported filters...



// Applying filter to identify images based on their extensions...

static FilenameFilter Image_Filter = new FilenameFilter() {
    public boolean accept(File f, String name)
    {
        for(String ext: extensions){
            if(name.endsWith("."+ ext)){
                return(true);
            }
        }
        return(false);
    }
    };

    public static void goThroughDirectories(String path)
    {

        }

    public static void main(String[] args) {

          String path = "C:\\Users\\kasharma\\Desktop\\Travelocity R8.3_8.3.0.apk\\res";


            for (File file : f.listFiles(Image_Filter)) 
                {
                if (f.isDirectory()) goThroughDirectories(path+f.getName());

                BufferedImage img = null;

                try {
                    img = ImageIO.read(file); 
                    System.out.println("image "+ file.getName());
                } catch (IOException e) {
                    // handle errors here
                }

        }
        }
    }

查看java.nio.files ,尤其是walkFileTree(...)find(...)方法。 Java 8為此提供了內置功能。

使用walkFileTree

public static void main(String[] args) throws Exception 
{
    Path p = Paths.get("D:/");
    Files.walkFileTree(p, 
            new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException
                {
                    System.out.println(file.toFile().getName());
                    return FileVisitResult.CONTINUE;
                }
            }
        );
}

這是使用find的一個更好的解決方案,它同時返回延遲填充的流和.jpg過濾器:

public static void main(String[] args) throws Exception
{
    Path p = Paths.get("D:/");
    Files
        .find(
            p, 
            Integer.MAX_VALUE, 
            (path,attr) -> path.toString().endsWith(".jpg"))
        .forEach(path -> System.out.println(path.toFile().getName()));
}

這會給你的想法。 這是偽代碼。

void goThroughDirectories(String path)
{
    for(File f : fileList)
    {
        if(f.isDirectory()) goThroughDirectories(path+f.getName());
        else {
                //do something
            }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM