Java Text File String Open Character Processing Question

Asked 2 years ago, Updated 2 years ago, 78 views

Currently, I'm planning to read and process characters from a text file (using bufferedreader)

I'm good at getting single-line ones, but if there's an opening character in the middle, is there any way to supplement this because the content after the opening character goes to the next line? (ex. Kanadara Mabasaaza) *: Opening character) What I want is a single string of Kanadara Mavasaaja, and the result is Kanadara Mavasaaja.

bufferedreader java newline

2022-09-22 20:25

1 Answers

Add each line as below (reduce).

jshell is available from jdk 9 and above.

allinux@yhjung:~$ cat out.txt
abcde
fegfhi
jklmn

jshell> import java.io.*
jshell> import java.util.stream.*

jshell> BufferedReader reader = new BufferedReader(new FileReader("out.txt"))
reader ==> java.io.BufferedReader@a7e666

jshell> Stream<String> lines = reader.lines()
lines ==> java.util.stream.ReferencePipeline$Head@68bbe345

jshell> String result = lines.reduce((a, b) -> a + b).get()
result ==> "abcdefegfhijklmn"

jshell> reader.close()


2022-09-22 20:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.