关于IO实现用户名 密码存取的问题

aa805617894aa 2010-04-11 11:01:01
import java.io.*;
public class Login implements Serializable{
Login b;
public String username;
public String password;
public Login(String user,String pword) {
username = user;
password = pword;
}
public String getUser() {
return username;
}
public String getPasswored() {
return password;
}
public void addUser(Login a) {
try {
ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream("login.txt"));
oo.writeObject(a);
}catch(IOException e) {
e.printStackTrace();
}
}
public Login testUser() {

try{
ObjectInputStream in = new ObjectInputStream(new FileInputStream("login.txt"));
b = (Login)in.readObject();
}catch(IOException e) {
e.printStackTrace();
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
return b;
}
}帮我看看吧 我想实现用户名 密码的存取
注册窗口的时候new Login(text1.gettext(),text2.gettext());
然后添加。
登录窗口的是后掉testUser()读然后实现登录;
现在毛病是只能先注册在登录; 直接登录读原来.txt的不行出空指针错误 ;
呵呵 帮我看看吧 谢谢了
...全文
108 4 打赏 收藏 转发到动态 举报
写回复
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
24K純帥 2010-04-11
  • 打赏
  • 举报
回复
试下1L的程序还有问题没??
wd9053 2010-04-11
  • 打赏
  • 举报
回复
没看明白你说的时什么,不过你的IO流都没有关闭
import java.io.*;
public class Login implements Serializable{
Login b;
public String username;
public String password;
public Login(String user,String pword) {
username = user;
password = pword;
}
public String getUser() {
return username;
}
public String getPasswored() {
return password;
}
public void addUser(Login a) {
try {
ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream("login.txt"));
oo.writeObject(a);
oo.flush();//////
oo.close();////////
}catch(IOException e) {
e.printStackTrace();
}
}
public Login testUser() {

try{
ObjectInputStream in = new ObjectInputStream(new FileInputStream("login.txt"));
b = (Login)in.readObject();
in.close();//////////////////////
}catch(IOException e) {
e.printStackTrace();
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
return b;
}
}
无聊司马 2010-04-11
  • 打赏
  • 举报
回复
还不是很懂这个问题
alvin198761 2010-04-11
  • 打赏
  • 举报
回复
用那个类干吗??
java.util.Propertise 类比那个简单啊
它有一键一值 可以直接保存到xml中(不需要别的 调用想过的方法就可以)
读的时候将它从xml中加载进来 (一样是一个方法)
它可以像集合(Map)一样使用
你看看 文档
相关推荐

62,567

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告