• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

write records to a file

 
Ranch Hand
Posts: 76
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone fill me in on how to write records to a file?
I have this code

that writes fine to a file. But I want more than one piece of info in this file. Do I have to create a record class?
Thanks
J
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
There are a few ways of writing data to a file. It really depends on how you want the data to be written.
The above code is the easiest way. If you want to write more info, use the same pw.print or pw.println() methods with the other info.
You could also chose to serialize the Objects and write them to the file.
for eg.
class T implements Serializable
{
....///....
your stuff
}
T t1 = new T();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File("your file")));
out.writeObject(t1);
This will write the object out to the file,
0To read back the object, use
ObjectInputStream out = new ObjectInputStream(new FileInputStream(new File("your file")));
T t1 = (T) out.readObject();
Hope this helps.
Ashwin.
 
Jay Brass
Ranch Hand
Posts: 76
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ashwin.
That was almost too easy. I guess I can stop work on that record class full of char []'s now huh?
J
 
Jay Brass
Ranch Hand
Posts: 76
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Ashwin or anyone,
The above works great if I only want one record in any give file. But what if I want more than one record? If I put in account number, firstname, last name and balance, how do I put in a new record in the same file with a new account number name and balance that doesn't overwrite the one already there? Do I have to pw.println them all at once as one long string?
Maybe I should start work on that record class again.
any thoughts??
thx
J
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jay,
I am almost sure, one can write any number of objects using successive calls to writeObject().
For example,

Also, if you want to just append few more records to an already existing file, you can open the underlying FileOutputStream with an append flag( see the documentation for overloaded constructor of FileOutputStream ).
Though I haven't tried this( writing/reading multiple objects ) myself, it should be possible. After all, what good it is if java limits one object per serialized file!!

Hope that helps,
Ajith
 
Jay Brass
Ranch Hand
Posts: 76
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
writeobject, append? hhhmmmmmm sounds logical. OK, I'll give it a whirl. I don't think I will shelve that record class just yet though.
Thanks Ajith
J
 
Hey, sticks and stones baby. And maybe a wee mention of my stuff:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic