views:

27

answers:

1

Hello,

I am trying to use Java's Desktop / getDesktop() / open() call to open a text (simple ASCII) file with the system's default editor. I am using FileWriter / PrintWriter to write to the file, flush it, close it, but I get am getting an exception thrown.

  1. I am using the same File object in my desktop.open(savefile) as I am in my FileWriter fw = new FileWriter(savefile) call.

  2. I can open any other type of file with no problem, including other text files.

  3. If I suspend the program immediately after writing and replace the outputted file with a file created with a text editor, THEN select "open" on my dialog, it opens perfectly... it just seems my text files aren't being created with the correct header or meta information that allows Desktop to know what to do with it.

Any assistance or experience would be appreciated. Is there an "official" file writer to use when simply dealing with text?

A: 

Sorry, can't reproduce your issue here with this basic kickoff example:

File file = new File("/test.txt");
FileWriter writer = null;
try {
    writer = new FileWriter(file);
    writer.write("test");
} finally {
    if (writer != null) writer.close();
}
Desktop.getDesktop().open(file);

Your problem lies somewhere else. Maybe you're closing the wrong Writer handle (you should be closing the "outermost" writer, e.g. if you wrapped a FileWriter in a BufferedWriter, you should close BufferedWriter instead) or doing it too late (e.g. calling Desktop#open() inside try while you're doing close in finally)?

BalusC
I'm doing all of this correctly, perhaps it is a system dependent thing.. I am developing on Linux, I will try it on a windows box and see what happens.
Ryan
Another update - it's actually working perfectly in debug mode with breakpoints in Netbeans, but when I run it for real it fails... just thought it might be noteworthy.
Ryan