如何根据Java中多个路径的文件名对文件进行分组

贝贝

我想根据来自多个路径的文件名对特定文件进行分组。我已经关注了这个 stackoverflow链接在开始流式传输路径以查找特定文件名后,我无法遍历每个文件。

以下是文件内容的路径:

/var/tmp/data_sample1/data2_first_example.set.csv
/var/tmp/data_sample1/data3_first_example.set.csv
/var/tmp/data_sample1/data1_first_example.set.csv
/var/tmp/data_sample2/data2_second_example.set.csv
/var/tmp/data_sample2/data1_second_example.set.csv
/var/tmp/data_sample2/data3_second_example.set.csv
/tmp/csv_files/data_sample3/data2_third_example.set.csv
/tmp/csv_files/data_sample3/data1_third_example.set.csv
/tmp/csv_files/data_sample3/data3_third_example.set.csv

枚举类:

enum PersonType {
    A,
    B
}

文件名.java

import java.util.Arrays;
import java.util.List;

public class FileName {
    private final String first = "_first_sample";
    private final String second = "_second_sample";
    private final String third = "_third_sample";

    private final List<String> filenames = Arrays.asList(first, second, third);
    public List<String> getFilenames() {
        return filenames;
    }
}

CSV文件.java

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

public class CSVFiles {

    private PersonType personType;
    private List<String> fileNames = new ArrayList<>();
    private List<File> firstSample = new ArrayList<>();
    private List<File> secondSample = new ArrayList<>();
    private List<File> thirdSample = new ArrayList<>();

    public CSVFiles(PersonType personType, List<String> paths) {
        if (personType == PersonType.A) {
            this.personType = personType;
            FileName fileName = new FileName();
            this.fileNames = fileName.getFilenames();
            setCSVFiles(paths);
        }
    }

    public List<File> setCSVFiles(List<String> paths) {
        List<Path> collect = paths.stream()
                .flatMap(path -> {
                    try {
                        return Files.find(Paths.get(path), Integer.MAX_VALUE,
                                (p, attrs) -> attrs.isRegularFile()
                                        && p.toString().contains(".set")
                                        && p.toString().endsWith(".csv")
                        );
                    } catch (IOException ex) {
                        throw new UncheckedIOException(ex);
                    }
                }).collect(Collectors.toList());

        return collect.stream()
                .map(Path::toFile)
                .filter(file -> {
                    if (file.getName().contains("_first_sample")) {
                        firstSample.add(file);
                        return true;
                    } else if (file.getName().contains("_second_sample")) {
                        secondSample.add(file);
                        return true;
                    } else if (file.getName().contains("_third_sample")) {
                        thirdSample.add(file);
                        return true;
                    }
                    return false;
                })
                .collect(Collectors.toList());
    }
}

CSVFilesTest.java

import org.junit.Test;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

public class CSVFilesTest {

    @Test
    public void test() {
        String data_sample1 = "/var/tmp/data_sample1";
        String data_sample2 = "/var/tmp/data_sample2";
        String data_sample3 = "/tmp/csv_files/data_sample3";
        List<String> paths = Arrays.asList(data_sample1, data_sample2, data_sample3);
        System.out.println(paths);

        CSVFiles csvFiles = new CSVFiles(PersonType.A, paths);
    }
}

期望输出:

firstSample: [data1_first_example.set.csv, data2_first_example.set.csv, data3_first_example.set.csv] 
secondSample: [data1_second_example.set.csv, data2_second_example.set.csv, data3_second_example.set.csv]
thirdSample:  [data1_third_example.set.csv, data2_third_example.set.csv, data3_third_example.set.csv]

任何反馈表示赞赏!

贝贝

解决方案感谢“同步”评论:

    public Map<String, List<String>> setCSVFiles(List<String> paths) {
        List<Path> collect = paths.stream()
                .flatMap(path -> {
                    try {
                        return Files.find(Paths.get(path), Integer.MAX_VALUE,
                                (p, attrs) -> attrs.isRegularFile()
                                        && p.toString().contains(".set")
                                        && p.toString().endsWith(".csv")
                        );
                    } catch (IOException ex) {
                        throw new UncheckedIOException(ex);
                    }
                }).collect(Collectors.toList());

        return collect.stream()
                .map(Path::toString)
                .collect(Collectors.groupingBy(path ->
                        path.substring(path.lastIndexOf("/")+1)
                        ));
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从包含绝对文件路径的字符串中获取文件名?

如何在Java中获取文件名和路径?

根据文件名中间的版本号对文件进行排序

如何在ruby中对文件名进行排序?

在不同目录中查找文件并对文件名进行操作

需要帮助根据文件名中的日期戳对文件列表进行排序

根据文件大小以及打印文件名和大小对文件(包括子目录中的文件)进行排序

如何考虑文件名对文件名进行重命名

如何以文件名中包含的整数的升序对文件进行排序

根据python中的文件名对文件进行排序

C ++在目录中对文件名进行排序

Bash:根据文件名对文件进行排序

如何在Powershell脚本中解析相对或绝对文件名输入参数的绝对路径?

根据其各自的文件扩展名对文件路径进行排序

如何按文件名对文件路径列表进行排序?

如何根据文件名查找文件的路径?

仅给定文件名,如何按时间对文件列表进行排序

Java构建API,如何根据文件名进行不同的文件处理?

如何对文件名中的空格进行排序并连接文件?

如何按文件名中的日期对文件排序?

Bash-根据多个字段对文件名进行排序

如何对多个文件名部分上的文件进行排序?

根据文件名的相似性对文件排序

根据文件名与文件夹Python匹配对文件进行排序

如何根据文件名创建多个目录并在linux中更改文件名?

python中的正则表达式根据匹配的文件名的开头和结尾对文件进行分组

对文件名进行 SQL 修剪

根据文件名中嵌入的时间戳对文件进行排序、分组和处理

如何使用 python 的文件扩展名对目录中的文件名进行分组?