Question: ```
    read_data = new BufferedReader( new FileReader(args[0]) );
    data_buffer = new StringBuffer();

    int i;

    while(read_data.ready())
    {           
        while((i = read_data.read()) != -1)
        {
            data_buffer.append((char)i);
        }           
    }

    data_buffer.append(System.getProperty("line.separator"));

```

What I'm trying to do is, read an entire .txt file into a string and append a newline to the string. And then be able to process this string later on by creating a new Scanner by passing data\_buffer.toString(). Obviously on really large files this process takes up a lot of time, and all I want to do is just append a newline to the .txt file I've read into memory.

I'm aware the whole idea seems a bit hacky or weird, but are there any quicker methods?

Cheers :)

Answer: 
From what I can tell, what you are *actually* trying to do is to read a file in such a way that it always appears to have a line separator at the end of the last line.

If that is the case, then you could do this by implementing a subtype of `FilterReader`, and have it "insert" an extra character or two if required when it reaches the end of the character stream.

The code to do this won't be trivial, but it will avoid the time and space overhead of buffering the entire file in memory.
If all you're doing is passing the resulting file in to a Scanner, you should create a Readable for the file and send that to Scanner.

Here's an example (untested):

```
public class NLReader implements Readable {

    Reader r;
    boolean atEndOfReader = false;
    boolean atEnd = false;

    public NLReader(Reader r) {
        this.r = r;
    }

    public int read(CharBuffer cb) throws IOException {
        if (!atEndOfReader) {
            int result = r.read(cb);
            if (result == -1) {
                atEndOfReader = true;
            } else {
                return result;
            }
        }
        if (!atEnd) {
            String nl = System.getProperty("line.separator");
            cb.append(nl);
            atEnd = true;
            return nl.length();
        }

        return -1;
    }
}

```

This only reads the file once, and never copies it (unlike your StringBuffer -- and you should be using StringBuilder instead unless you really need the synchronization of StringBuffer).

This also doesn't load the actual file in to memory, so that can save memory pressure as well.