Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Help needed

  • 01-08-2008 6: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,916 ✭✭✭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,088 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,916 ✭✭✭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,401 ✭✭✭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