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: 
The fastest way to do something is often to not do it at all.

Why don't you modify the parsing code in such way that the newline at the end is not required? If you are appending it each time, you could as well change the code to behave as if it were there while it really isn't.

The next thing I would try would be to avoid creating a huge String char by char, as this is indeed rather costly. You can create a `Scanner` based on an `InputStream` and it will probably be much faster than reading data into a `String` and parsing that. You can override your `FileInputStream` to return a virtual newline character at the end of the file, thus avoiding the instatiation of the pasted string.

And if you absolutely positively did have to read the data into a buffer, you would probably be better off by reading into a byte array using the array-based `read()` methods of the stream - much faster than byte by byte. Since you can know the size of the file in advance, you could allocate your buffer with space for the extra end-of-line marker and insert it into the array. In contrast to creating a `StringBuffer` and making a `String` out of it, this does not require a full copy of the buffer.
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.