Optimum time to perform an operation: within, or after loop
===

I am reading a file to parse later on. The file is not likely to exceed an MB in size, so this is perhaps not a crucial question for me at this stage. But for best practise reasons, I'd like to know when is the optimum time to perform an operation.

Example:

I am reading a buffer into a string and would like to remove all whitespace. My method is currently thus:

    private String listRaw;
    
    private boolean readList(String filePath) throws java.io.IOException {
        StringBuilder fileData = new StringBuilder(1000);
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        char[] buf = new char[1024];
        int numRead;
        while((numRead = reader.read(buf)) != -1){
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
            buf = new char[1024];
        }
        reader.close();
        listRaw = fileData.toString().replaceAll("\\s", "");
        return true;
    }

So, I remove all whitespace from the string at the time I store it - in it's entirety - to a class variable.

To me, this means less processing but more memory usage. Would I be better off applying the `replaceAll()` operation on the `readData` variable as I append it to `fileData` for best practise reasons? Using more processing but avoiding passing superfluous whitespace around.

I imagine this has little impact for a small file like the one I am working on, but what if it's a 200MB log file?

Is it entirely case-dependant, or is there a consensus I'd do better to follow?