Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Wrapping my head around writing to files  RSS feed

 
Mark Runals
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All,

I am having trouble wrapping my head around how you go about writing/appending info to a file. Am not having a whole lot of luck finding a good example to pull apart on the web. What I am able to find basically creates a file and types "Hello World". I need something just a bit beyond that. Ultimately I have 6500 records from a data base where pieces of info will be written to one of several flat files as the individual items have program logic applied to them. For simplicity's sake I created the following bit of code. If I could get some help filling in the gaps I would appreciate it. Basically I want to write even numbers to one file and odd to another.


Thanks in advance.


Edit by mw: Added Code Tags.
[ August 15, 2007: Message edited by: marc weber ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

This Java I/O Tutorial should help you get started.
 
Mark Runals
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks marc. Wish the post would hold tabs to help preserve the readability. I came up with the following (that works). RandomAccessFile seems to be the way to go vs creating many lines of code just to establish a connection to a file as I had seen in the examples I had found. Is there a downside or cases where it wouldn't be the thing to use?

Oh - should note I used writeChars because in the end I am dealing with strings and just saw I had use it vs writeInt or something more appropriate given my array (basically I wasn't paying enough attention to this code lol).



Edit by mw: Added Code Tags.
[ August 15, 2007: Message edited by: marc weber ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably don't need a RandomAccessFile, because it doesn't look like you need non-sequential (random) file access. For the above example of writing sequences out to files, I might just use a simple FileWriter (see the Character Streams page).

Also, you should include your close() calls inside the try block, in case there is some problem closing.
[ August 15, 2007: Message edited by: marc weber ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12541
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Runals:
Thanks marc. Wish the post would hold tabs to help preserve the readability.

when you post your code, you can either surround it with
[ CODE]
<your code here>
[ /CODE]

(without the spaces), or highlight it and click that little "code" button you see just below, in that "instant UBB Code" field next to the smilies.

that will preserve the spacing in that block.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!