java 根据文件名查找文件路径遍历所有文件

import java.io.File;

public class DiskManage {
    static boolean IS_USE = false;

    private DiskManage() {
    }

    public static File[] getLogicDisks() {
        //所有可用文件系统根目录的根目录
        return File.listRoots();
    }

    public static void main(String[] args) {
        File[] arFileRoot = DiskManage.getLogicDisks();
        //循环查找每个盘下所有文件
        for (File file : arFileRoot) {
            showList(file);
        }
        if (!IS_USE) {
            System.out.println("未找到匹配文件");
        }
    }

    private static void showList(File file) {
        if (file.isDirectory()) {//如果是目录
            File[] listFiles = file.listFiles();//获取当前路径下的所有文件和目录,返回File对象数组
            if (listFiles != null) {
                for (File f : listFiles) {//将目录内的内容对象化并遍历
                    showList(f);
                }
            }
        } else if (file.isFile()) {//如果是文件,并且存在
            if (file.getPath().contains("你想要的查找的文件名")) {
                IS_USE = true;
                System.out.println("文件:" + file.getPath());
            }
        }
    }
} 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以使用多线程来查找文件。以下是一个简单的示例代码: ```java import java.io.File; import java.util.ArrayList; import java.util.List; public class FileSearch implements Runnable { private String fileName; private String searchDir; private List<String> result; public FileSearch(String fileName, String searchDir) { this.fileName = fileName; this.searchDir = searchDir; this.result = new ArrayList<>(); } @Override public void run() { search(new File(searchDir)); } private void search(File file) { if (file.isDirectory()) { File[] files = file.listFiles(); if (files != null) { for (File f : files) { search(f); } } } else { if (file.getName().equals(fileName)) { result.add(file.getAbsolutePath()); } } } public List<String> getResult() { return result; } } ``` 在上面的代码中,我们定义了一个名为`FileSearch`的类,实现了`Runnable`接口。在`run`方法中,我们使用递归的方式遍历指定目录下的所有文件文件夹。 当遍历文件时,我们将文件名与指定的要查找的文件名进行比较,如果相同,则将该文件的绝对路径添加到结果列表中。 使用时,可以创建多个`FileSearch`对象,并将其作为线程启动。每个线程将在指定目录下查找指定文件,并将结果保存在各自的结果列表中。 以下是一个示例的使用代码: ```java public class Main { public static void main(String[] args) { String searchDir = "path/to/search/directory"; String fileName = "file_to_search.txt"; FileSearch search1 = new FileSearch(fileName, searchDir); FileSearch search2 = new FileSearch(fileName, searchDir); Thread thread1 = new Thread(search1); Thread thread2 = new Thread(search2); thread1.start(); thread2.start(); try { thread1.join(); thread2.join(); } catch (InterruptedException e) { e.printStackTrace(); } List<String> result1 = search1.getResult(); List<String> result2 = search2.getResult(); System.out.println("Search results from thread 1:"); for (String path : result1) { System.out.println(path); } System.out.println("Search results from thread 2:"); for (String path : result2) { System.out.println(path); } } } ``` 在上面的代码中,我们创建了两个`FileSearch`对象,并分别启动了两个线程进行搜索。最后,我们输出了每个线程的搜索结果。 注意:以上代码仅为示例,实际使用时可能需要添加异常处理、文件过滤等逻辑。另外,多线程查找文件时,要注意线程安全性和资源竞争的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值