Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BufferedReader: read multiple lines into a single string

I'm reading numbers from a txt file using BufferedReader for analysis. The way I'm going about this now is- reading a line using .readline, splitting this string into an array of strings using .split

public InputFile () {
    fileIn = null;

    //stuff here

    fileIn = new FileReader((filename + ".txt"));
    buffIn = new BufferedReader(fileIn);


    return;
    //stuff here
}

public String ReadBigStringIn() {
    String line = null;

    try { line = buffIn.readLine(); }
    catch(IOException e){};

    return line;
}

public ProcessMain() {
    initComponents();
    String[] stringArray;
    String line;

    try {
        InputFile stringIn = new InputFile();
        line = stringIn.ReadBigStringIn();
        stringArray = line.split("[^0-9.+Ee-]+"); 
        // analysis etc.
    }
}

This works fine, but what if the txt file has multiple lines of text? Is there a way to output a single long string, or perhaps another way of doing it? Maybe use while(buffIn.readline != null) {}? Not sure how to implement this.

Ideas appreciated, thanks.

like image 452
S_Wheelan Avatar asked Apr 01 '11 16:04

S_Wheelan


People also ask

How many lines can BufferedReader read?

The readLine() method of BufferedReader class in Java is used to read one line text at a time.

Which method of BufferedReader class can take input of a single character?

The read() method of BufferedReader class in Java is used to read a single character from the given buffered reader. This read() method reads one character at a time from the buffered stream and return it as an integer value.

Does BufferedReader read line by line?

Java Read File line by line using BufferedReaderWe can use java. io. BufferedReader readLine() method to read file line by line to String. This method returns null when end of file is reached.

Is BufferedReader faster than FileReader?

As BufferedReader uses buffer internally, this class is much faster than FileReader. BufferReader doesn't need to access the hard drive every time like FileReader and hence faster.


2 Answers

You are right, a loop would be needed here.

The usual idiom (using only plain Java) is something like this:

public String ReadBigStringIn(BufferedReader buffIn) throws IOException {
    StringBuilder everything = new StringBuilder();
    String line;
    while( (line = buffIn.readLine()) != null) {
       everything.append(line);
    }
    return everything.toString();
}

This removes the line breaks - if you want to retain them, don't use the readLine() method, but simply read into a char[] instead (and append this to your StringBuilder).

Please note that this loop will run until the stream ends (and will block if it doesn't end), so if you need a different condition to finish the loop, implement it in there.

like image 173
Paŭlo Ebermann Avatar answered Sep 20 '22 21:09

Paŭlo Ebermann


I would strongly advice using library here but since Java 8 you can do this also using streams.

    try (InputStreamReader in = new InputStreamReader(System.in);
         BufferedReader buffer = new BufferedReader(in)) {
        final String fileAsText = buffer.lines().collect(Collectors.joining());
        System.out.println(fileAsText);
    } catch (Exception e) {
        e.printStackTrace();
    }

You can notice also that it is pretty effective as joining is using StringBuilder internally.

like image 21
Grzegorz Gajos Avatar answered Sep 17 '22 21:09

Grzegorz Gajos