2023.1.4日学习内容(遍历文件夹)

1,遍历电脑文档,把遍历的文档的路径添加到一个新的txt文件里

public class SeeAll {

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

Set<String> set = in();

System.out.println("======================");

output(set);

}

/**

* 创建新文件夹,把set集合里取到的路径名送到文件夹里的txt文件里

*/

private static void output(Set<String> set) throws IOException {

File file = new File("C:\\Users\\19191\\Desktop\\text1");

file.mkdirs();

File fileAddress = new File(file, "fileAddress.txt");

fileAddress.createNewFile();

FileOutputStream fos = new FileOutputStream("C:\\Users\\19191\\Desktop\\text1\\fileAddress.txt");

for (String s : set) {

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

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

}

fos.close();

}

/**

* 查找文件夹并且将文件地址录入set集合

*/

private static Set in() {

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

File[] first = listRoots();

for (File file : first) {

System.out.println(file);

}

while (true) {

Scanner scanner = new Scanner(System.in);

System.out.println("请输入进入查找的项目");

String address = scanner.next();

if (address.equals("return")) {

in();

}

if (address.equals("exit")) {

break;

}

if (address.length() < 2) {

address += ":\\";

} else {

address = "\\" + address;

}

File file = new File(address);

if (!file.isDirectory()) {

System.out.println("该文件不是文件夹或该文件夹为空,不再进入");

System.out.println("请重新输入查找的项目");

File[] second = listRoots();

for (File file1 : second) {

System.out.println(file1);

}

continue;

}

File[] nowFile = file.listFiles();

assert nowFile != null;

for (File file1 : nowFile) {

System.out.println(file1.getAbsoluteFile());

set.add(String.valueOf(file1.getAbsoluteFile()));

}

}

return set;

}

}

2,复制文档

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

Scanner scanner = new Scanner(System.in);

System.out.println("请输入想要复制的文件路径");

String oldAddress = scanner.next();

File file = new File(oldAddress);

System.out.println("请输入想将文件路径复制到何处");

String addressNew = scanner.next();

//遍历该地址下的的文件

find(file, addressNew);

}

private static void find(File file, String addressNew) throws IOException {

File[] file1 = file.listFiles();

for (File father : file1) {

String allName = father.getAbsolutePath();

String endName = father.getName();

if (father.isFile()) {

//输入的地址是此文件的绝对路径 "D:\\test\\新建 BMP 图像.bmp"

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(allName)) ;

//输出的地址是参数传来的路径+\\+此文件名 "D:\\test1"+"\\"+"新建 BMP 图像.bmp"

BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream(addressNew + "\\" + endName));

byte[] bys = new byte[1024];

int len;

while ((len = bis.read(bys)) != -1) {

bos.write(bys, 0, len);

}

bos.close();

bis.close();

} else {

//如果是文件夹,就创建一个相同的文件夹在参数传来的目的地处

File child = new File(addressNew + "\\" + father.getName());

child.mkdirs();

//将新创建的文件夹,和此文件夹的地址作为参数返回,此文件夹的地址就是我们新传文件的地址

find(father, child.getAbsolutePath());

}

}

}

}

3,查找该文件下所有文件的后缀名种类与个数

public class Test {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("请输入您查找的地址");

String address = scanner.next();

File file = new File(address);

Map<String, Integer> map = new HashMap<>();

find(map, file);

map.forEach((s, i) -> System.out.println("后缀名为:" + s + "有" + i + "个"));

}

private static void find(Map<String, Integer> map, File file) {

File[] child = file.listFiles();

assert child != null;

for (File f : child) {

if (f.isFile()) {

String endName = f.getName().split("\\.")[1];

Integer count = map.get(endName);

map.put(endName, count == null ? 1 : count + 1);

} else {

find(map, f);

}

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值