Home arrow static arrow Java Programming [Archive] - Java doesn't writes to disk
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Java doesn't writes to disk
2 Duke Stars available
This topic has 4 replies on 1 page.

Posts:49
Registered: 6/17/04
Java doesn't writes to disk  
Jun 25, 2004 9:44 AM



 
Well I m the same guy who posted the Java doesn't write to disk topic

Well the probelm is Data is not written correctly and if not written it cannot be readen from therecan U post me a method which can write and read from the disk the following thing
class User implements Serializable{ private String UserName; private String Password;  public User(String usr, String pwd){ UserName = usr; Password = pwd; } public User(){ }  public User(User usr){ this.UserName = usr.UserName; this.setPassWord(usr.Password); }   public String getUserName(){ return UserName; } public void setPassWord(String pwd){ Password = pwd; } public boolean equalto(User usr){ if(this.UserName == usr.UserName) return true; else return false;  } } 

I m running it as a GUI application and it takes the UserName and password from the textfields............
and two buttons Create User and Login
The Login one reads the data
and the New User obviously writes it

The file is being created here but the data is not written correctly
The following is the read method which reads the data
 void usrlgn(ActionEvent e) {  User myUsr = new User(userName.getText(), passWord.getText());   Expense expense = new Expense();  if (isUsr(myUsr))   expense.setVisible(true);   else     JOptionPane.showMessageDialog(LoginExpense.this, "Cannot read a from file"                                           ,"Error", JOptionPane.ERROR_MESSAGE);  }   }

The isUsr() Method is as follows..............
public boolean isUsr(User usr){User checkusr = new User(usr);User usr2 = new User(); try{    FileInputStream fis = new FileInputStream("C:/Program Files/expense.users");    ObjectInputStream in = new ObjectInputStream(fis);     usr2 = (User)in.readObject();    in.close();       }       catch(IOException d){         JOptionPane.showMessageDialog(LoginExpense.this, d.toString()                                           ,"Error", JOptionPane.ERROR_MESSAGE);              }          catch(Exception d){JOptionPane.showMessageDialog(LoginExpense.this, "Some other reasons effected it"                                           ,"Error", JOptionPane.ERROR_MESSAGE);                 }    if (usr2.equalto(checkusr))      return true;    else      return false;    }and the code that writes to disk is as follows (remember I m using a Windows enviroment).void newusr(ActionEvent e) {    boolean flag = false;    flag = isUsr(user); try{       FileOutputStream fos = new FileOutputStream("C:/Program Files/expense.users", true);      ObjectOutputStream out = new ObjectOutputStream(fos);   if(!flag){      out.writeObject(user);      out.flush();      out.close();      JOptionPane.showMessageDialog(LoginExpense.this, "User Created", "Success",                                        JOptionPane.PLAIN_MESSAGE);      userName.setText("");      passWord.setText("");      }    else       JOptionPane.showMessageDialog(LoginExpense.this, "User already exists", "Failure",                                        JOptionPane.PLAIN_MESSAGE);                                  out.close();    }    catch(IOException f){   JOptionPane.showMessageDialog(LoginExpense.this, "Cannot create a new User"                                           ,"Error", JOptionPane.ERROR_MESSAGE);   }    catch(Exception f){       JOptionPane.showMessageDialog(LoginExpense.this, "Cannot create a new User"                                           ,"Error1", JOptionPane.ERROR_MESSAGE);  } }

Help me with this thing.............................
 

Posts:21,719
Registered: 98-02-20
Re: Java doesn't writes to disk  
Jun 25, 2004 10:05 AM (reply 1 of 4)



 
Posted before...

http://forum.java.sun.com/thread.jsp?thread=532993&forum=31&message=2571457

%
 

Posts:1,577
Registered: 00-02-25
Re: Java doesn't writes to disk  
Jun 25, 2004 10:16 AM (reply 2 of 4)



 
Well the probelm is Data is not written correctly and
if not written it cannot be readen from therecan U
post me a method which can write and read from the
disk the following thing

My suggestion is that you first write a command-line program that writes an object out to disk and reads it back in. That will verify for you that ObjectOutputStream and ObjectInputStream work. Once you've verified that they work, then you can start to think about why they don't appear to work in your GUI.

And if they don't work for you with a simple command-line program (it should be a dozen lines of code), then post that simple program here so that people can comment on it without being distracted by mounds of GUI and application-specific code.
 

Posts:21,719
Registered: 98-02-20
Re: Java doesn't writes to disk  
Jun 25, 2004 10:31 AM (reply 3 of 4)



 
Like this:

import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable; class User implements Serializable{    public static final String DEFAULT_FILE_NAME = "User.ser"private String username;    private String password;     public static void main(String [] args)    {        try        {            if (args.length == 1)            {                FileInputStream fis = new FileInputStream(DEFAULT_FILE_NAME);                ObjectInputStream ois   = new ObjectInputStream(fis);                 User user = (User)ois.readObject();                ois.close();                 System.out.println(user);            }            else if (args.length == 2)            {                User user = new User(args[0], args[1]);                FileOutputStream fos = new FileOutputStream(DEFAULT_FILE_NAME);                ObjectOutputStream oos = new ObjectOutputStream(fos);                oos.writeObject(user);                oos.flush();                oos.close();            }            else            {                System.out.println("Read in an object by giving a file path");                System.out.println("Write out an object by giving a username and password");            }        }        catch (Exception e)        {            e.printStackTrace();        }    }     public User()    {        this("", "");    }     public User(String usr, String pwd)    {        this.username = usr;        this.password = pwd;    }     public User(User usr)    {        this(usr.getUsername(), usr.getPassword());    }      public String getUsername() { return this.username; }     public String getPassword() { return this.password; }     public void setUsername(final String newUsername)    {        this.username = newUsername;    }     public void setPassword(final String pwd)    {        this.password = pwd;    }     public boolean equals(Object o)    {        if (this == o)            return trueif (!(o instanceof User))            return false;         User u = (User)o;         boolean sameUsername = this.username.equals(u.getUsername());        boolean samePassword    = this.password.equals(u.getPassword());         return (sameUsername && samePassword);    }     public int hashCode()    {        int value = 7;         value       = 11*value + 13*this.username.hashCode();        value       = 17*value + 19*this.username.hashCode();         return value;    }     public String toString()    {        return this.username + ":" + this.password;    }}


Works perfectly for me.

%
 

Posts:21,719
Registered: 98-02-20
Re: Java doesn't writes to disk  
Jun 25, 2004 10:31 AM (reply 4 of 4)



 
Sorry - I should have put javadoc comments in there.

%
 
This topic has 4 replies on 1 page.