2023.1.5日学习内容(遍历文件夹升级版)

1,判断文件夹是否为空

1)设定一个返回类型为boolean的方法

2)判断文件夹是否存在file==null,若为 null则返回false

3)用list方法将文件夹的子路径存放在String类型的数组里

4)判断数组是否存在或数组的长度小于0,代码如下

f (file == null) {

return false;

}

String[] s = file.list();

return s == null || s.length <= 0;

2,代码实现一个本地文件搜索

实现思路

1)获取盘地址(listRoots)

2)遍历每个盘的内容,如果选中的是文件,就记录文件的名称到集合中,如果选中的是文件夹就再次遍历(若文件夹为空则会出现空指针异常,就要用到我们上面提到的方法来判断是否为空文件夹)

3)新建一个txt文件,将获取到的集合写在文件里并且换行

4)键入搜索词,查询input读到的内容中是否包含搜索词

5)打印出包含的搜索词

注意

1,注意选中内容的三个状态,是文件,是空文件夹,是有内容的文件夹

2,注意读内容时每行每行的读,若不是每行每行的读就会产生拼接的情况,为了提高准确性就要用到BufferedReader字节缓冲流的特有方法readLine(),readLine()的返回值时一个String集合,而read则是int类型

代码如下

搜索的代码

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

String name = scanner.next();

find(name);

}

private static void find(String name) {

try {

FileReader fr = new FileReader("a.txt");

BufferedReader br = new BufferedReader(fr);

String line = null;

while ((line = br.readLine()) != null) {

if (line.contains(name)) {

System.out.println(line);

}

}

br.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

创建txt的代码

public static void main(String[] args) {

Long startTime = System.currentTimeMillis();

File[] father = File.listRoots();

Set<String> address = new TreeSet<>();

address.addAll(seeAllChild(father, address));

addInFile(address);

Long endTime = System.currentTimeMillis();

System.out.println(endTime - startTime);

}

//创建文件夹并且把集合中的地址输入

private static void addInFile(Set<String> address) {

File saveAddress = new File("a.txt");

try {

saveAddress.createNewFile();

} catch (IOException e) {

e.printStackTrace();

}

try {

FileOutputStream fos = new FileOutputStream("a.txt");

for (String s : address) {

fos.write("\r\n".getBytes(StandardCharsets.UTF_8));

fos.write(s.getBytes(StandardCharsets.UTF_8));

}

fos.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

//遍历C,D,E,F盘

private static Set<String> seeAllChild(File[] father, Set<String> address) {

Set<String> set = new TreeSet<>();

for (File fileChild : father) {

set.addAll(childAdd(fileChild, set));

}

return set;

}

//遍历盘中的文件

private static Set<String> childAdd(File fileChild, Set<String> set) {

File[] childList = fileChild.listFiles();

for (File file : childList) {

if (file.isFile() || isDirectoryEmpty(file)) {

// set.add(file.getAbsolutePath() + "\\" + file.getName());

set.add(file.getName());

} else {

childAdd(file, set);

}

}

return set;

}

private static boolean isDirectoryEmpty(File file) {

if (file == null) {

return false;

}

String[] s = file.list();

return s == null || s.length <= 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值