• Post Reply Bookmark Topic Watch Topic
  • New Topic

IOStreams - Writing & Reading to a File  RSS feed

 
Christian Long
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm new here & to Java also - I'm learning as quickly as I can.
But I'm havin understanding problems with 'File Handling':

I need to use a 'File'(.txt/.dat) to store records of customers.
I hav just learned 2 Kind of Files: 'Sequential' & 'Random Access'

1. Firstly, can someone tell me which of the 2 files I need to use?
2. For the 'Sequential' file: I hav learned this type of code:

File file1 = new File("data1.dat");
DataOutputStream out = new DataOutputStream(new FileOutputStream(file1) ;
out.writeUTF("An example \n Same Example");
out.close();

* The problem is that by using 'FileOutputStream', it overwrites everything in the File - if I write something to the File again.
1.Is there a way to just add something at the end instead of 'overwriting' the data? or Should I use a Random Access file? (or some other type of file?).


[ October 04, 2007: Message edited by: mistaken myst ]
[ October 04, 2007: Message edited by: mistaken myst ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15862
81
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"mistaken myst", please check your private messages. You can see them by clicking My Private Messages.
 
Rob Spoor
Sheriff
Posts: 20895
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FileOutputStream also has constructors with a boolean with which you can specify to append to or overwrite the file. Check those.
 
Danilo Dadonas
Ranch Hand
Posts: 59
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case, in my oppinion, ObjectOutputStream is better than DataOutputStream and FileOutputStream. With ObjectOutputStream you can store the object customer, not a record like a database record. Look the following example:

In this case you're storing the objects.


Here you're loading the array of objects from the file.
 
Christian Long
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok - Firstly Thnx 4 both answers.

Danilo Dadonas - I think your code is what I will especially be needing.

Anyway I will need to check both answers' code quickly & will be putting back an answer by the end of the day to tell if it worked /or if I'm havin some problems.

 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Random access is really neat when all your records have exactly the same length. Then you can "seek" your way to a record by record number or overwrite a record in the file without rewriting the whole thing. But fixed length records are not that common so random access is probably something you can conveniently forget for a while.

The whole IO class hierarchy is big and a bit challenging. The IO chapter in Bruce Eckell's (free online book) Thinking In Java is good, but you have to read and thoroughly digest pretty much every sentence. I still use some file IO libraries I wrote while reading his book on my first Java assignment.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!