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.
-
I am using the same File object in my
desktop.open(savefile)as I am in myFileWriter fw = new FileWriter(savefile)call. -
I can open any other type of file with no problem, including other text files.
-
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?
Sorry, can’t reproduce your issue here with this basic kickoff example:
Your problem lies somewhere else. Maybe you’re closing the wrong
Writerhandle (you should be closing the “outermost” writer, e.g. if you wrapped aFileWriterin aBufferedWriter, you should closeBufferedWriterinstead) or doing it too late (e.g. callingDesktop#open()insidetrywhile you’re doing close infinally)?