Win a copy of Securing DevOps this week in the Security forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Best Way to Write an Array To A File  RSS feed

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
never mind all looks good in WordPad.

Hopefully this helps someone out.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

maybe can you try change this

to this?


it's a wild guess though..

Regards,
Jon
 
Mike Osterhout
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should you use println() method instead? it will automatically append the correct newline character.
 
Jon Kho
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
perhaps you want to try this?

Regards,
Jon
 
Sheriff
Posts: 57927
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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).
 
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
Thread Boost - a very different sort of advertising
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!