Moe Moe - 10 months ago 50
Java Question

BufferedReader and stream Line Java 8

Working with BufferedReader: I have a text file that has similar lines as listed below.
Text File line sample:


How can I remove the end of the line char
and join the next line/Field to the first line to have a new line then store it in an array to print later.

What I want to have is to able to join the 2 lines if
is found at end of the 1st line then assign each element of the "2 lines" (Now ONE LINE) that is separated by the delimiter " " to Fields where I can call later to print. but I Also want to remove the unwanted characters
found at the end of the line.

Here is what I want to have as the new combined line to store in an array

Field-1 Field-2 Field-3 Field-4 Field-5 Field-6;

Where the first line of the new array will be equal to

Field-1 = ABC Field-2 = DEF Field-3 = EFG Field-4 = Field-5 = -Ba Field-6 =;

the new combined line (2 in one ) will be generated if
char is found at the end of the first line.

what I have thus far in the Bufferedreader class

public class ReadFile {

private String path;

ReadFile(String filePath) {
path = filePath;

public String[] OpenFile() throws IOException {
FileReader fr = new FileReader(path);
BufferedReader textReader = new BufferedReader(fr);

int numberOfLines = readLines();
String[] textData = new String[numberOfLines];

for (int i = 0; i < numberOfLines; i++) {
textData[i] = textReader.readLine();


return textData;

//Not sure if It's better to have while loop instead of this to reach end of file, let me know what you think?
int readLines() throws IOException {
FileReader f2r = new FileReader(path);
BufferedReader bf = new BufferedReader(f2r);
String aLine;
int numberOfLines = 0;
while ((aLine = bf.readLine()) != null) {

return numberOfLines;


This would read a text file and concatenate any lines which end with a '\' with the following line.

Two important notes here, this assumes that the input is correct and that the \ character is the last character in the line (you would have to sanitize the input if this is not true), and that the last line of the file does not end with a backslash.

try (Bufferedreader br = new BufferedReader(new FileReader(file))) {
    String line;
    StringBuilder concatenatedLine = new StringBuilder();
    List<String> formattedStrings = new ArrayList<String>();
    while((line = br.readLine()) != null){

        //If this one needs to be concatenated with the next,
        if( line.charAt(line.length() -1) == '\\' ){ 
            //strip the last character from the string
            line = line.substring(0, line.length()-1); 
            //and add it to the StringBuilder
        //If it doesn't, this is the end of this concatenated line
            //Add it to the formattedStrings collection.
            //Clear the StringBuilder

    //The formattedStrings arrayList contains all of the strings formatted for use.