javaIO流如何读取文件夹下所有文件

import java.io.*;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Map;

import java.util.List;

public class exchange {

private static Map<String, Map<String,Long>> map = new HashMap();

public static void main(String[] args) throws IOException {

File file = new File("/demo/file");

findPath(file);

//存文件名字

List<String> list = new ArrayList<>();

for(Map.Entry<String, Map<String, Long>> each : map.entrySet()){

list.add(each.getKey());

Map<String, Long> temp = each.getValue();

String path = null;

for(Map.Entry<String,Long> m : temp.entrySet()){

path = m.getKey();

}

System.out.println("文件名:\t"+ each.getKey() + "\t\t\t\t路径:\t"+ path);

}

}

//递归寻找

public static void findPath(File f){

if(f != null){

//获得文件夹下面的所有文件和文件并且返回File数组

File[] files = f.listFiles();

if(files != null){

for(File file : files) {

if (file.isFile()) {//如果是文件那么就操作

Map<String, Long> temp = new HashMap();

temp.put(file.getAbsolutePath(), file.length());//存入绝对文件路径和文件大小

map.put(file.getName(), temp);//文件名和封装好的map对象

} else {

//不是文件那么说明是文件夹,继续递归往后找

if(file != null)

findPath(file);

}

}

}

}

return ;

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值