View Single Post
  #1   Spotlight this post!  
Unread 03-04-2013, 07:16 PM
joelg236 joelg236 is offline
4334 Retired Mentor & Alumni
AKA: Joel Gallant
no team
Team Role: Mentor
 
Join Date: Dec 2011
Rookie Year: 2012
Location: Calgary
Posts: 733
joelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond repute
"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?
__________________
All opinions are my own.
Reply With Quote