Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    schvuu is offline Member
    Join Date
    Oct 2013
    Posts
    15
    Rep Power
    0

    Default write & read/load to/from file

    Hello, I need to write some objects to a file, then when I 'read' them back to the program, they have to be exactly the same, they have to be the same kind of objects, with the same name and everything, I have tried several different approches, but it all fails, the objects are 'accounts' for a bank (run over rmi)

    Java Code:
        	AccountServer KAC= new AccountServer("Jane");
        	AccountServer JAC = new AccountServer("John");
    
        	Naming.rebind("Jane",KAC);	
        	KAC.setName("Jane");
        	KAC.setNumber(1234);
        	KAC.setBalance(600);
        
        	Naming.rebind("John",JAC);
        	JAC.setName("John");
        	JAC.setNumber(2345);
        	JAC.setBalance(50);
    The important thing here is the 'Naming.rebind' is has to become "Jane"/"John" or whatever the name is, becouse the bank client finds the account using 'Naming'
    Any thoughts? I am loosing my mind here

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: write & read/load to/from file

    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: write & read/load to/from file

    I don't get this question. It is about file reading/writing, yet none of the posted code actually does something with a file.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: write & read/load to/from file

    "but it all fails"

    In what way does it fail?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    schvuu is offline Member
    Join Date
    Oct 2013
    Posts
    15
    Rep Power
    0

    Default Re: write & read/load to/from file

    I'm sorry for being unclear, but the posted code is the code I want to write to a file, and then 'read' upon starting my server, I have tried adding these 'accounts' to an arraylist, then serializing it and writing to file with 'FileOutputStream' , but when i read it into the program again, I don't know how to get them to be exactly as they were.

    This is a piece of the client

    Java Code:
            String username = JOptionPane.showInputDialog("Enter Name");
            AccountClient.account = (Account)Naming.lookup(
                "rmi://" + serverAddress + "/"+username);
    the user enters the name and the client uses 'Naming.lookup' to find they right account.

    But when I 'read' from file, Naming.lookup can't find the account.
    Last edited by schvuu; 11-26-2013 at 04:12 PM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: write & read/load to/from file

    Make a small complete program that compiles, executes and shows the problem. Have the program create some sample objects with some data, write those objects to a file, read those objects from the file and do the lookup you want.
    Hardcode whatever data you need so no other files are needed.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: write & read/load to/from file

    What does your reading code look like then?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    schvuu is offline Member
    Join Date
    Oct 2013
    Posts
    15
    Rep Power
    0

    Default Re: write & read/load to/from file

    I want some way to write to file, then read it back exactly as it were, but something changes and I don't know why.
    I've been reading on oracle, and watching videos on writing/reading to file, then implementing what they did to my program, but I doesn't work.

  9. #9
    schvuu is offline Member
    Join Date
    Oct 2013
    Posts
    15
    Rep Power
    0

    Default Re: write & read/load to/from file

    Quote Originally Posted by Norm View Post
    Make a small complete program that compiles, executes and shows the problem. Have the program create some sample objects with some data, write those objects to a file, read those objects from the file and do the lookup you want.
    Hardcode whatever data you need so no other files are needed.
    yea, ehm I'm not by any meens any good at java, but I will try i guess

  10. #10
    schvuu is offline Member
    Join Date
    Oct 2013
    Posts
    15
    Rep Power
    0

    Default Re: write & read/load to/from file

    Quote Originally Posted by Tolls View Post
    What does your reading code look like then?
    This is the one that worked best
    Java Code:
    		try{ FileInputStream Acc= new FileInputStream("test.sav");
    		ObjectInputStream reader = new ObjectInputStream(Acc); 
    		AccountServer x = new AccountServer(); 
    		x = (AccountServer) reader.readObject(); 
    		Naming.rebind("Jane",x);
    But as you can see, I had to write "Naming.rebind("Jane",x);"
    So I don't think this would work when I have alot of accounts on the same file

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: write & read/load to/from file

    Then you need more practice writing code. We need to see a program that is trying to do what you want done so we can help you get the techniques for doing it.

    You need to post a complete program, not just a few lines of code that uses undefined methods.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    schvuu is offline Member
    Join Date
    Oct 2013
    Posts
    15
    Rep Power
    0

    Default Re: write & read/load to/from file

    Quote Originally Posted by Norm View Post
    Then you need more practice writing code. We need to see a program that is trying to do what you want done so we can help you get the techniques for doing it.

    You need to post a complete program, not just a few lines of code that uses undefined methods.
    well, can I just post the whole program then? server & client?

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: write & read/load to/from file

    If there are less than 300 lines total. After that it gets too big.

    Remove any code that is NOT part of this problem. Hardcode values in the code instead of needing to read data from a file or requiring user input.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: write & read/load to/from file

    Quote Originally Posted by schvuu View Post
    well, can I just post the whole program then? server & client?
    Are you sure that you can? If a company owns that code, you should not be posting it on the net or you might get into big trouble.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: write & read/load to/from file

    Quote Originally Posted by schvuu View Post
    This is the one that worked best
    Java Code:
    		try{ FileInputStream Acc= new FileInputStream("test.sav");
    		ObjectInputStream reader = new ObjectInputStream(Acc); 
    		AccountServer x = new AccountServer(); 
    		x = (AccountServer) reader.readObject(); 
    		Naming.rebind("Jane",x);
    But as you can see, I had to write "Naming.rebind("Jane",x);"
    So I don't think this would work when I have alot of accounts on the same file
    But you have to rebind it.
    How else is the naming server supposed to know what "Jane" is bound to?
    And you can only save the account stuff, not the state of whatever naming server you're using.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  16. #16
    schvuu is offline Member
    Join Date
    Oct 2013
    Posts
    15
    Rep Power
    0

    Default Re: write & read/load to/from file

    Quote Originally Posted by Norm View Post
    If there are less than 300 lines total. After that it gets too big.

    Remove any code that is NOT part of this problem. Hardcode values in the code instead of needing to read data from a file or requiring user input.
    Well, I can just show you an example here
    Java Code:
        public static void CreateAcc() throws FileNotFoundException, IOException{
            //create new accounts 
        	AccountServer janeAcc= new AccountServer("Jane");
        	AccountServer johnAcc = new AccountServer("John");
    
        	Naming.rebind("Jane",janeAcc);	
        	janeAcc.setName("Jane");
        	janeAcc.setNumber(1234);
        	janeAcc.setBalance(600);
        
        	Naming.rebind("John",johnAcc);
        	johnAcc.setName("John");
        	johnAcc.setNumber(2345);
        	johnAcc.setBalance(50);
        	
        	System.out.println("Registered accounts.");
        	
            try {
    
                // create a new file with an ObjectOutputStream
                FileOutputStream out = new FileOutputStream("test.txt");
                ObjectOutputStream oout = new ObjectOutputStream(out);
    
                // write to the file
                oout.writeObject(janeAcc);
                oout.writeObject(johnAcc);
    
                // close the stream
                oout.close();
    
             } catch (Exception ex) {
                ex.printStackTrace();
             }
        }
    How can I read this back to the program, so that they get the same value as they have before.
    The methods I have used have to enter a name when you read it back, when you enter that name, the 'account' gets a new name, not as in SetName, but in Naming.rebind("Jane",janeAcc);

    If the "Jane" in Naming.rebind("Jane",janeAcc); changes, they can't access the account from the client:

    Java Code:
            String username = JOptionPane.showInputDialog("Enter Name");
            AccountClient.account = (Account)Naming.lookup(
                "rmi://" + serverAddress + "/"+username);
    "username" is checked against accounts "Naming.lookup"

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: write & read/load to/from file

    How does that name change?
    The name in your naming needs to be consistent, so you can rebind it properly.

    How would "Jane" change?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  18. #18
    schvuu is offline Member
    Join Date
    Oct 2013
    Posts
    15
    Rep Power
    0

    Default Re: write & read/load to/from file

    Quote Originally Posted by Tolls View Post
    How does that name change?
    The name in your naming needs to be consistent, so you can rebind it properly.

    How would "Jane" change?
    That's what I don't know, but when I run the client after 'reading in accounts' they can't find "Jane" or "John"

    Can someone show me how to properly read test.txt into the program?
    Like.. Does it matter if it's test.txt och test.ser ?

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: write & read/load to/from file

    Read in some sample data (as above, with the two accounts).
    Then print out the accounts and check the values are correct.

    That's part 1.

    Part 2 is to load those accounts into the naming server.
    Check that they are loaded by attempting to read them.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  20. #20
    schvuu is offline Member
    Join Date
    Oct 2013
    Posts
    15
    Rep Power
    0

    Default Re: write & read/load to/from file

    Quote Originally Posted by Tolls View Post
    Read in some sample data (as above, with the two accounts).
    Then print out the accounts and check the values are correct.

    That's part 1.

    Part 2 is to load those accounts into the naming server.
    Check that they are loaded by attempting to read them.
    I went after this example: Java.io.ObjectOutputStream.writeObject(Object obj) Method Example

    Java Code:
            FileOutputStream out = new FileOutputStream("test.txt");
            ObjectOutputStream oout = new ObjectOutputStream(out);
    
            oout.writeObject(janeAcc);
            oout.writeObject(johnAcc);
    
            oout.close();
    
            ObjectInputStream ois =
                    new ObjectInputStream(new FileInputStream("test.txt"));
    
            System.out.println("" + (AccountServer) ois.readObject());
            System.out.println("" + ois.readObject());
    That printed:
    AccountServer[UnicastServerRef [liveRef: [endpoint:[192.168.0.105:55568](local),objID:[-1ee88635:1429522d253:-7ff8, -5247659413767959372]]]]
    AccountServer[UnicastServerRef [liveRef: [endpoint:[192.168.0.105:55568](local),objID:[-1ee88635:1429522d253:-7ff7, 2959223607179521153]]]]

    When I ran the client I got "java.rmi.NotBoundException"
    So i'm guessing I can't do it that way

Page 1 of 2 12 LastLast

Similar Threads

  1. read/write file
    By akl in forum New To Java
    Replies: 5
    Last Post: 07-14-2013, 09:53 PM
  2. Replies: 8
    Last Post: 07-25-2012, 11:00 AM
  3. Replies: 0
    Last Post: 07-25-2011, 06:16 PM
  4. Replies: 5
    Last Post: 02-05-2009, 11:28 AM
  5. Read and Write file
    By mrdestroy in forum New To Java
    Replies: 13
    Last Post: 10-31-2008, 01:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •