精细记录时将文件拆分为块(java 8) [英] Split file in chunk when fine head record (java 8)

查看:61
本文介绍了精细记录时将文件拆分为块(java 8)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码,可以在找到开始记录时将文件分成"几块.

I've a piece of code that "split" a file in some chunks when find a start record.

List<StringBuilder> list = new ArrayList<>();
StringBuilder jc = null;
try (BufferedReader br = Files.newBufferedReader(Paths.get("")) {
    for (String line = br.readLine(); line != null; line = br.readLine()) {
        if (line.startsWith("REQ00")) {
            jc = new StringBuilder();
            list.add(jc);
        }
        jc.append(line);
    }
} catch (IOException e) {
    e.printStackTrace();
}

有什么方法可以将此代码转换"为Java 8 Stream方法?

Is there any way to "convert" this code into Java 8 Stream way ?

推荐答案

为作业使用正确的工具.使用Scanner,就像

Use the right tool for the job. With Scanner, it’s as simple as

List<String> list = new ArrayList<>();
try(Scanner s = new Scanner(Paths.get(path))) {
    s.useDelimiter(Pattern.compile("^(?=REQ00)", Pattern.MULTILINE));
    while(s.hasNext()) list.add(s.next());
} catch (IOException e) {
    e.printStackTrace();
}

现在,您的代码具有创建StringBuilder且不保留换行符的特殊要求.因此,扩展版本为:

Now your code has the special requirements of creating StringBuilders and not retaining the line breaks. So the extended version is:

List<StringBuilder> list = new ArrayList<>();
try(Scanner s = new Scanner(Paths.get(path))) {
    s.useDelimiter(Pattern.compile("^(?=REQ00)", Pattern.MULTILINE));
    while(s.hasNext()) list.add(new StringBuilder(s.next().replaceAll("\\R", "")));
} catch (IOException e) {
    e.printStackTrace();
}

更有效的变体是

List<StringBuilder> list = new ArrayList<>();
try(Scanner s = new Scanner(Paths.get(path))) {
    s.useDelimiter(Pattern.compile("^(?=REQ00)", Pattern.MULTILINE));
    while(s.hasNext()) list.add(toStringBuilderWithoutLinebreaks(s.next()));
} catch (IOException e) {
    e.printStackTrace();
}

…

static final Pattern LINE_BREAK = Pattern.compile("\\R");
static StringBuilder toStringBuilderWithoutLinebreaks(String s) {
    Matcher m = LINE_BREAK.matcher(s);
    if(!m.find()) return new StringBuilder(s);
    StringBuilder sb = new StringBuilder(s.length());
    int last = 0;
    do { sb.append(s, last, m.start()); last = m.end(); } while(m.find());
    return sb.append(s, last, s.length());
}

从Java 9开始,还可以对其使用Stream操作:

Starting with Java 9, you can also use a Stream operation for it:

List<StringBuilder> list;
try(Scanner s = new Scanner(Paths.get(path))) {
    list = s.useDelimiter(Pattern.compile("^(?=REQ00)", Pattern.MULTILINE))
            .tokens()
            .map(string -> toStringBuilderWithoutLinebreaks(string))
            .collect(Collectors.toList());
} catch (IOException e) {
    e.printStackTrace();
    list = List.of();
}

这篇关于精细记录时将文件拆分为块(java 8)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆