PDA

Click to See Complete Forum and Search --> : Binary File


Neptune
Jul 14th, 2003, 09:55 AM
Hi there.

I am using the byte oriented stream for reading and writing data from my file. From what i understand, we use writeInt() for writing and readInt() for reading from file. This also applies to other primitive data types as well. How about String? Can anyone pls explain to me how it works? Besides, we can create binary file and accessing them. but how to delete them? what code should be used?

Thank u. :)

Dillinger4
Jul 14th, 2003, 11:10 AM
If you want to delete a file check out the public boolean delete() method in the java.io.File class. As for reading Strings i pretty sure that the readLine() method in the in the DataInputStream class has been deprecated. The only readLine() method that i know of is in the java.io.BufferedReader class. The DataInputStream class does define a readUTF() method though.

Neptune
Jul 15th, 2003, 12:03 PM
Thank u so much for the solution. Really appreciate that.

Sincerely,
Neptune.

CornedBee
Jul 15th, 2003, 01:57 PM
You could also use the object streams.

Neptune
Jul 15th, 2003, 10:40 PM
I've tried on object stream. Unfortunately, i m quite confusing with it because my revision book touch less on it and the resources from the net makes me confusing also. I m new Java. less than 2 months. there are a lot of stream. really makes me headache at first. anyway, with all your help, i manage to solve the problem.
thanks for ur advise, CornedBee.

Neptune.

CornedBee
Jul 16th, 2003, 01:04 AM
What you do is:

ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(filename));
int i = 13254;
char c = 's';
double d = 239.14609;
String s = "Jumanji";

oos.writeInt(i);
oos.writeChar(c);
oos.writeDouble(d);
oos.writeObject(s);

Neptune
Jul 16th, 2003, 11:09 AM
thanks alot CornedBee. after doing some research and guides given by u, i manage to understand object streams.

by the way, may i ask u something?
We create 2 different object's with data member(like name, age and etc) from same class, eg Person.
When comes to storing, the 2 object particulars is store inside (a) or (b)
a) into two different .dat file or
b) one .dat file

Can u pls explain to me?

sincerely,
Neptune.

CornedBee
Jul 16th, 2003, 02:04 PM
Into one.

Neptune
Jul 16th, 2003, 09:12 PM
If that is the case, then what if i want to read the second data from the .dat file? how to do that? can u explain?

Neptune.

CornedBee
Jul 17th, 2003, 02:56 AM
I guess you would have to read it sequentially, in the same order you stored it.
So


// file contains in this order:
// a java.awt.Rectangle
// a java.lang.String
// an int
// an int
// a java.lang.String

// To get the second string, you must do:
// ObjectInputStream ois;
ois.readObject();
ois.readObject();
ois.readInt();
ois.readInt();
String wanted = (String)ois.readObject();


AFAIK there can be no successful seeking in object files.

Neptune
Jul 17th, 2003, 11:16 AM
Thank you Corned Bee.

Then i think i would go for storing of 1 object's data member in 1 single .dat file instead of all into 1 file.

any comment, pls reply.

Neptune.

CornedBee
Jul 17th, 2003, 03:44 PM
Why would you need to load one specific object?

Neptune
Jul 17th, 2003, 09:31 PM
it's for editing purpose. besides storing object, it should also allow deleting of object. U know, save person, delete, and edit person's object.
If there r lots of object in a single file, it would have makes me headache. 1 to 1 is easier to maintain. perhaps yes, if more object means the data file would be more. but what to do, that's the only method i know. plus, i m lack of time. any advise?

Neptune.

CornedBee
Jul 18th, 2003, 01:57 AM
The idea is that you have one file for each set of data, that is, situations you want to load seperatly.

But if one data set contains several objects (strings, ints, whatever), those are all stored in one file.

Neptune
Jul 18th, 2003, 03:07 AM
Now i get it. perhaps both of us have misunderstood each other.
I want to ask something for final confirmation.
if we have the 2 Person object, eg:
1. A1(String name, int age, char sex, String address)
2. A2(String name, int age, char sex, String address)
So we save 2 particulars(set of data) in 2 different data file? or in 1 data file. which 1 is recommended and easier to maintain?

Thanks CornedBee.

Sincerely,
Neptune.

CornedBee
Jul 18th, 2003, 03:16 AM
That depends on what you want to do. If you want to load and manipulate each person individually, place them in different files. If you always load and save them together, place them in one file.

Or use something completly different, like an XML file. I have some classes at home (I'm at work now) that give you the means to use the object streams with xml files: two Base64 streams and two DomText text streams.

Basically you have to create a DOM Text/CDATA node and then you can write to it like this:
ObjectOutputStream oos = new ObjectOutputStream(new Base64OutputStream(new DOMTextNodeWriter(node)));

which then writes the objects as Base64 encoded binary data to the XML file, which you can then save.

Neptune
Jul 18th, 2003, 04:33 AM
Thanks for ur information CornedBee.
i think i would prefer carry on with my existing code.
really appreciate ur help and advise.

regards,
Neptune.

Neptune
Jul 22nd, 2003, 04:39 AM
Hello.
does anyone knows how to determine the number of files(let say data files) inside a directory?

tq,
Neptune.

CornedBee
Jul 22nd, 2003, 09:34 AM
You should create a new thread for an unrelated question.

But since you already asked...

java.io.File dir = new java.io.File(pathname);
int count = dir.list().length;