"File Closed" when writing to log file


    private static DataOutputStream logFile() throws IOException {
        if (logFile == null || !logFile.isOpen() || !logFile.exists()) {
            if(logFile != null) {
                logFile.close();
            }
            logFile = (FileConnection) Connector.open(PATH, Connector.READ_WRITE);
            logFile.create();
            outputStream = logFile.openDataOutputStream();
        }
        return outputStream;
    }

    public static void logFile(String msg) throws IOException {
        System.out.println(msg);
        appendToFile(msg, logFile());
    }

    public static void appendToFile(String msg, DataOutputStream outputStream) throws IOException {
        if (msg == null || outputStream == null) {
            throw new NullPointerException();
        }
        try {
            outputStream.write(msg.getBytes());
            outputStream.close();
        } catch (IOException ex) {
            System.err.println("Error while printing " + msg + "
	" + ex.getMessage());
        }
    }

This code outputs the first log sent to it properly, but every subsequent time logs:

[cRIO] Error while printing test message
[cRIO]
[cRIO] Stream closed

I assumed the stream should always be open (since I open it whenever the stream !isOpen()). But it’s not.

Any idea why this is happening?

EDIT: Solved. I was stupid and put outputStream.close(); every time it appends to the file…