英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论