Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   "File Closed" when writing to log file (http://www.chiefdelphi.com/forums/showthread.php?t=114652)

joelg236 03-04-2013 07:16 PM

"File Closed" when writing to log file
 
Code:

    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 + "\n\t" + ex.getMessage());
        }
    }

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

[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?

joelg236 03-04-2013 08:02 PM

Re: "File Closed" when writing to log file
 
EDIT: Solved. I was stupid and put outputStream.close(); every time it appends to the file...


All times are GMT -5. The time now is 08:56 AM.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi