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

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

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>.

  1. @SneakyThrows
  2. @Override
  3. public Flux<String> getLines() {
  4. final InputStream inputStream = new ClassPathResource(pathToFile).getInputStream();
  5. final InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
  6. final BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
  7. return Flux.using(
  8. bufferedReader::lines,
  9. Flux::fromStream,
  10. stringStream -> {
  11. close(inputStreamReader);
  12. close(bufferedReader);
  13. stringStream.close();
  14. }
  15. );
  16. }
  17. private void close(final Closeable closeable) {
  18. try {
  19. closeable.close();
  20. } catch (final IOException e) {
  21. throw new RuntimeException(e);
  22. }
  23. }

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

EDIT:
I refactored it to:

  1. return Flux.using(
  2. () -> new ClassPathResource(pathToFile).getInputStream(),
  3. is -> Flux.using(
  4. () -> new InputStreamReader(is),
  5. isr -> Flux.using(
  6. () -> new BufferedReader(isr),
  7. br -> Flux.using(
  8. br::lines,
  9. Flux::fromStream,
  10. BaseStream::close
  11. ),
  12. this::close
  13. ),
  14. this::close
  15. ),
  16. this::close
  17. )
英文:

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>

  1. @SneakyThrows
  2. @Override
  3. public Flux<String> getLines() {
  4. final InputStream inputStream = new ClassPathResource(pathToFile).getInputStream();
  5. final InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
  6. final BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
  7. return Flux.using(
  8. bufferedReader::lines,
  9. Flux::fromStream,
  10. stringStream -> {
  11. close(inputStreamReader);
  12. close(bufferedReader);
  13. stringStream.close();
  14. }
  15. );
  16. }
  17. private void close(final Closeable closeable) {
  18. try {
  19. closeable.close();
  20. } catch (final IOException e) {
  21. throw new RuntimeException(e);
  22. }
  23. }

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

EDIT:
I refactored it to:

  1. return Flux.using(
  2. () -> new ClassPathResource(pathToFile).getInputStream(),
  3. is -> Flux.using(
  4. () -> new InputStreamReader(is),
  5. isr -> Flux.using(
  6. () -> new BufferedReader(isr),
  7. br -> Flux.using(
  8. br::lines,
  9. Flux::fromStream,
  10. BaseStream::close
  11. ),
  12. this::close
  13. ),
  14. this::close
  15. ),
  16. this::close
  17. )

答案1

得分: 1

我重构了它成这样:

  1. return Flux.using(
  2. () -> new ClassPathResource(pathToFile).getInputStream(),
  3. is -> Flux.using(
  4. () -> new InputStreamReader(is),
  5. isr -> Flux.using(
  6. () -> new BufferedReader(isr),
  7. br -> Flux.using(
  8. br::lines,
  9. Flux::fromStream,
  10. BaseStream::close
  11. ),
  12. this::close
  13. ),
  14. this::close
  15. ),
  16. this::close
  17. )

它正常运行。

英文:

I refactored it to this:

  1. return Flux.using(
  2. () -> new ClassPathResource(pathToFile).getInputStream(),
  3. is -> Flux.using(
  4. () -> new InputStreamReader(is),
  5. isr -> Flux.using(
  6. () -> new BufferedReader(isr),
  7. br -> Flux.using(
  8. br::lines,
  9. Flux::fromStream,
  10. BaseStream::close
  11. ),
  12. this::close
  13. ),
  14. this::close
  15. ),
  16. this::close
  17. )

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:

确定