使用反应器逐行读取文本文件。

huangapple go评论67阅读模式
英文:

Read text file line by line with reactor

问题

I want to read a file using Flux. Basically what I am trying to do is to convert text file inside my spring-boot jar to Flux<String>.

@SneakyThrows
@Override
public Flux<String> getLines() {
    final InputStream inputStream = new ClassPathResource(pathToFile).getInputStream();
    final InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
    final BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

    return Flux.using(
        bufferedReader::lines,
        Flux::fromStream,
        stringStream -> {
            close(inputStreamReader);
            close(bufferedReader);
            stringStream.close();
        }
    );
}

private void close(final Closeable closeable) {
    try {
        closeable.close();
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

I am not sure if I am doing this right, and how to improve that (however it works properly)

EDIT:
I refactored it to:

return Flux.using(
    () -> new ClassPathResource(pathToFile).getInputStream(),
    is -> Flux.using(
        () -> new InputStreamReader(is),
        isr -> Flux.using(
            () -> new BufferedReader(isr),
            br -> Flux.using(
                br::lines,
                Flux::fromStream,
                BaseStream::close
            ),
            this::close
        ),
        this::close
    ),
    this::close
)
英文:

I want to read a file using Flux. Basically what I am trying to do is to convert text file inside my spring-boot jar to Flux<String>

@SneakyThrows
@Override
public Flux<String> getLines() {
    final InputStream inputStream = new ClassPathResource(pathToFile).getInputStream();
    final InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
    final BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

    return Flux.using(
        bufferedReader::lines,
        Flux::fromStream,
        stringStream -> {
            close(inputStreamReader);
            close(bufferedReader);
            stringStream.close();
        }
    );
}

private void close(final Closeable closeable) {
    try {
        closeable.close();
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

I am not sure if I am doing this right, and how to improve that (however it works properly)

EDIT:
I refactored it to:

return Flux.using(
    () -> new ClassPathResource(pathToFile).getInputStream(),
    is -> Flux.using(
        () -> new InputStreamReader(is),
        isr -> Flux.using(
            () -> new BufferedReader(isr),
            br -> Flux.using(
                br::lines,
                Flux::fromStream,
                BaseStream::close
            ),
            this::close
        ),
        this::close
    ),
    this::close
)

答案1

得分: 1

我重构了它成这样:

return Flux.using(
    () -> new ClassPathResource(pathToFile).getInputStream(),
    is -> Flux.using(
        () -> new InputStreamReader(is),
        isr -> Flux.using(
            () -> new BufferedReader(isr),
            br -> Flux.using(
                br::lines,
                Flux::fromStream,
                BaseStream::close
            ),
            this::close
        ),
        this::close
    ),
    this::close
)

它正常运行。

英文:

I refactored it to this:

return Flux.using(
    () -> new ClassPathResource(pathToFile).getInputStream(),
    is -> Flux.using(
        () -> new InputStreamReader(is),
        isr -> Flux.using(
            () -> new BufferedReader(isr),
            br -> Flux.using(
                br::lines,
                Flux::fromStream,
                BaseStream::close
            ),
            this::close
        ),
        this::close
    ),
    this::close
)

and it is working fine

huangapple
  • 本文由 发表于 2020年8月6日 20:35:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63283758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定