This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Notepad is one of the places where you notice that \n is not platform-independent. It requires the proper DOS/Windows line separator of "\r\n". WordPad obviously can convert \n to a line separator, as can quite a lot of other applications. You are better using a println() method, or
You need "finally" round the Writer, otherwise an Exception after it is created will prevent it from closing, and will keep the file in use and unavailable for other applications.
The customary way to do it with a BufferedWriter would look like this (old-fashioned style used)Of course, since Java 5 you would use a Formatter and the %n tag. The %n tag gives you the exact line separator for your operating system. This is much easier to use; you can test whether there are any problems at the end by saying
wroteOK = format.ioException() == null; That means you are returning "false" if an exception occurred, otherwise assume all is well and return true. You might still have to catch a FileNotFoundException when you create a File to pass to the Formatter, however. The only problem with Formatters is that you can't append (at least I have never found how to append with a Formatter).
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com