Results 1 to 19 of 19

Thread: Binary File

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    20

    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.

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    20
    Thank u so much for the solution. Really appreciate that.

    Sincerely,
    Neptune.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    20
    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.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    20
    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.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Into one.
    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.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    20
    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.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    20
    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.

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    20
    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.

  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    20
    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.

  16. #16
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    20
    Thanks for ur information CornedBee.
    i think i would prefer carry on with my existing code.
    really appreciate ur help and advise.

    regards,
    Neptune.

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    20
    Hello.
    does anyone knows how to determine the number of files(let say data files) inside a directory?

    tq,
    Neptune.

  19. #19
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width