Java读取文件路径和名称

读取文件夹内部的所有文件和文件名称

实现方法:通过递归函数来遍历文件,并将遍历的内容,保存到了数组中,并返回;方便后续的批量存入数据库。

import java.io.File;

public class readpath {
    //定义了一个全局变量,来存储递归函数内的文件路径
    static int len = 5000;
    //长度自己估摸着大概预设一个就好
    static int num = 0;
    static String[] paths = new String[len];

    public static void main(String[] args) {
        File file = new File("E:/**/**");
        paths =fileList(file, 0, paths);
        for(int i=0; i<file.length();i++){
            if(paths[i]!=null){
                System.out.println(paths[i]);
            }
        }
    }

    public static String[] fileList(File file, int node, String paths[]) {
        node++;
        File[] files = file.listFiles();
        if (files != null) {
            for (int n = 0; n<files.length;n++) {
                for (int i = 0; i < node; i++) {
                    //判断出为子目录
                    if (i == node - 2) {
                        paths[num] = files[n].getAbsolutePath();
                        num++;
                    }
                }
                //+file1.getName()仅获取文件名
                //System.out.println( files[n].getAbsolutePath());
                fileList(files[n], node, paths);
            }
        }
        return paths;
    }
}

部分运行结果如下:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Le05280

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值