|
-
Jul 14th, 2003, 09:55 AM
#1
Thread Starter
Junior Member
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.
-
Jul 14th, 2003, 11:10 AM
#2
Dazed Member
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.
Last edited by Dilenger4; Jul 14th, 2003 at 11:15 AM.
-
Jul 15th, 2003, 12:03 PM
#3
Thread Starter
Junior Member
Thank u so much for the solution. Really appreciate that.
Sincerely,
Neptune.
-
Jul 15th, 2003, 01:57 PM
#4
You could also use the object streams.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jul 15th, 2003, 10:40 PM
#5
Thread Starter
Junior Member
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.
-
Jul 16th, 2003, 01:04 AM
#6
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);
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jul 16th, 2003, 11:09 AM
#7
Thread Starter
Junior Member
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.
-
Jul 16th, 2003, 02:04 PM
#8
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jul 16th, 2003, 09:12 PM
#9
Thread Starter
Junior Member
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.
-
Jul 17th, 2003, 02:56 AM
#10
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jul 17th, 2003, 11:16 AM
#11
Thread Starter
Junior Member
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.
-
Jul 17th, 2003, 03:44 PM
#12
Why would you need to load one specific object?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jul 17th, 2003, 09:31 PM
#13
Thread Starter
Junior Member
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.
-
Jul 18th, 2003, 01:57 AM
#14
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jul 18th, 2003, 03:07 AM
#15
Thread Starter
Junior Member
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.
-
Jul 18th, 2003, 03:16 AM
#16
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jul 18th, 2003, 04:33 AM
#17
Thread Starter
Junior Member
Thanks for ur information CornedBee.
i think i would prefer carry on with my existing code.
really appreciate ur help and advise.
regards,
Neptune.
-
Jul 22nd, 2003, 04:39 AM
#18
Thread Starter
Junior Member
Hello.
does anyone knows how to determine the number of files(let say data files) inside a directory?
tq,
Neptune.
-
Jul 22nd, 2003, 09:34 AM
#19
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;
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|