-
Binary File
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. :)
-
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.
-
Thank u so much for the solution. Really appreciate that.
Sincerely,
Neptune.
-
You could also use the object streams.
-
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.
-
What you do is:
Code:
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);
-
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.
-
-
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.
-
I guess you would have to read it sequentially, in the same order you stored it.
So
Code:
// 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.
-
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.
-
Why would you need to load one specific object?
-
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.
-
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.
-
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.
-
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.
-
Thanks for ur information CornedBee.
i think i would prefer carry on with my existing code.
really appreciate ur help and advise.
regards,
Neptune.
-
Hello.
does anyone knows how to determine the number of files(let say data files) inside a directory?
tq,
Neptune.
-
You should create a new thread for an unrelated question.
But since you already asked...
Code:
java.io.File dir = new java.io.File(pathname);
int count = dir.list().length;