File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Best Way to Write an Array To A File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Best Way to Write an Array To A File" Watch "Best Way to Write an Array To A File" New topic
Author

Best Way to Write an Array To A File

Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
I am having problems with writing an array to a file.

Ideally I would like to have a function that could be passed an array, and that array would overwrite my file.

I know I have to start things off like this, but I am not sure how to actually write to the file.



I have tried to mimic the example in my book but it doesnt use an array. Any suggestions would be appreciated.
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Ok I spoke too soon. I just hunkered down and thought it out and I came up with this.



The problem is now everything prints on one line, Am I doing something wrong with the \n?
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
never mind all looks good in WordPad.

Hopefully this helps someone out.
Jon Kho
Ranch Hand

Joined: Jun 03, 2009
Posts: 54
Hi,

maybe can you try change this

to this?


it's a wild guess though..

Regards,
Jon
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
It appeared to be printing on one line b/c I was viewing the file in notepad. If you view it in wordpad the \n shows up.

Also I ended up changing that line just to tidy things up.
Duc Vo
Ranch Hand

Joined: Nov 20, 2008
Posts: 254
Should you use println() method instead? it will automatically append the correct newline character.


“Everything should be as simple as it is, but not simpler.” Albert Einstein
Jon Kho
Ranch Hand

Joined: Jun 03, 2009
Posts: 54
perhaps you want to try this?

Regards,
Jon
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
Jon Kho wrote:perhaps you want to try this?

Regards,
Jon
It has an improvement, using a finally to close the writer, but the bit about if (...) ... true ... else is dreadful style. You should simply write

pw = new PrintWriter(new FileWriter(fileName, appendToFile));

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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Best Way to Write an Array To A File