Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Help needed

  • 01-08-2008 07:37PM
    #1
    Closed Accounts Posts: 75 ✭✭


    i have this class to ecapsulate a worker and i want help creating a binary file in the main program using this class, not to sure how to combine the two


    class Worker {
    private String name;
    private double wage;

    Worker() {
    }

    Worker(String s, double w) {
    name = s; wage = w;
    }

    void put(DataOutputStream dos) {
    // write name & age attributes to dos (at file pointer)
    try {

    dos.writeUTF(name);
    dos.writeDouble(wage);
    } catch (IOException e) {
    }
    }

    void get(DataInputStream dis) {
    // read name & age attributes from file dis
    try {

    dis.readUTF();
    dis.readDouble();
    } catch (IOException e) {
    }
    }

    void put(){ // display on screen
    System.out.println(name + " " + wage);
    }
    }


Comments

  • Registered Users, Registered Users 2 Posts: 1,919 ✭✭✭ronivek


    Not entirely sure what it is you're trying to accomplish or what constraints you're working with; but I've included a small snippet of code below that demonstrates the general idea.

    All it does is effectively wrap a Data*Stream object around a Stream object; which in this case is a File*Stream object as it's a file you wish to work with.
    String fileName = "c:\\file.txt";
    try {      
        DataInputStream inputStream = new DataInputStream(new FileInputStream(fileName));
        DataOutputStream outputStream = new DataOutputStream(new FileOutputStream(fileName));
                
        worker.put(outputStream);
        worker.get(inputStream);            
    } catch (IOException e) { e.printStackTrace(); }
    


  • Moderators, Science, Health & Environment Moderators Posts: 10,093 Mod ✭✭✭✭marco_polo


    You must love typing ;) I can go whole line shorter ronvick :P
    fileName = "c:\\file.txt"
    try {      
        RandomAccessFile raf = new RandomAccessFile(fileName,"rw");
    
                
        worker.put(raf);
        worker.get(raf);            
    } catch (IOException e) { e.printStackTrace(); }
    


  • Registered Users, Registered Users 2 Posts: 1,919 ✭✭✭ronivek


    I don't think your code will even compile!

    The whole point of my example was to illustrate the use of Streams; although I probably didn't do such a good job.


  • Registered Users, Registered Users 2 Posts: 5,397 ✭✭✭DublinDilbert


    You will need to make sure your object is serialisable, from memory it must implement the serialisable interface.

    Once your object is serialisable you can write it to an object stream.

    So all your main program needs to do is open up a file, initalise an object stream, with the newly opened file. Then you can write your user defined objects to the object stream, hence they get stored to the file on the disk.


Advertisement