求教 文件保存用户信息,然后读取信息的方法 本人在info.data里保存拉用户的username和password,我怎样对其中的记录进行读取,,求高手指教!!要多少报酬也在所不惜!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 info.data的结构是什么样子的?从文件读进来,再按结构找us和pw 用文件流写入文件,可以用如下方式获取:try { pro=new Properties(); fr=getClass().getResourceAsStream("info.data"); pro.load(fr); }catch(FileNotFoundException e){System.out.println(e);}String username=pro.getProperty("user");String psw=pro.getProperty("password"); 我这里读取的代码没有实现循环,只能读取第一行,高手指点!!帖子完毕人人得分!! FileInputStream istream = new FileInputStream("userInfo.data"); ObjectInputStream in = new ObjectInputStream(istream); UserInfo info = (UserInfo) in.readObject(); if(txf_UserName.getText().equals(info.getUserName()) && tpf_Password.getText().equals (info.getUserPassword())){ yes = true; } istream.close();下面是info.data的添加行代码 try{ FileOutputStream ostream = new FileOutputStream("userInfo.data"); ObjectOutputStream p = new ObjectOutputStream(ostream); UserInfo tmp = new UserInfo("x", "x"); p.writeObject(tmp); tmp = new UserInfo("z", "z"); p.writeObject(tmp); p.flush(); ostream.close(); } catch (IOException e) {} 例子里用的是Object的序列化,读写的是一整个对象,无须再循环什么的了。 看样子username和userpassword都是UserInfo中的成员变量,类型是String 要读取的话像上面例子中给的一样,读文件、取一个对象出来强制成UserInfo的对象,然后再通过getUserName()和getUserPassword()你想要的值:======== FileInputStream istream = new FileInputStream("userInfo.data"); ObjectInputStream in = new ObjectInputStream(istream); UserInfo info = (UserInfo) in.readObject(); String username_I_want = info.getUserName(); String password_I_want = info.getUserPassword();========================== Any other question, please email to: [email protected] or visit my blog: toppi.blogchina.com 内存空间溢出!求大牛解释 java list排序问题 咨询上海程序员招聘会在哪里有 log4j方面的问题 330分求向数据库读、存图片的源码,有完整代码优先 请帮忙翻译一句话,是java-tutorial中的 大家来看看这2个程序要怎么理解 ButtonHandler handler = new ButtonHandler();这句话为什么出错呀? java的生存空间有多大? 请教各位,回答给分。。。。 JDBC连接问题????送30分!!!在线等!! 编译时出错,请高手指教
从文件读进来,再按结构找us和pw
try
{
pro=new Properties();
fr=getClass().getResourceAsStream("info.data");
pro.load(fr);
}
catch(FileNotFoundException e){System.out.println(e);}String username=pro.getProperty("user");
String psw=pro.getProperty("password");
FileInputStream istream = new FileInputStream("userInfo.data");
ObjectInputStream in = new ObjectInputStream(istream);
UserInfo info = (UserInfo) in.readObject();
if(txf_UserName.getText().equals(info.getUserName()) && tpf_Password.getText().equals (info.getUserPassword())){
yes = true;
}
istream.close();
下面是info.data的添加行代码
try{
FileOutputStream ostream = new FileOutputStream("userInfo.data");
ObjectOutputStream p = new ObjectOutputStream(ostream);
UserInfo tmp = new UserInfo("x", "x");
p.writeObject(tmp);
tmp = new UserInfo("z", "z");
p.writeObject(tmp);
p.flush();
ostream.close();
} catch (IOException e) {}
看样子username和userpassword都是UserInfo中的成员变量,类型是String
要读取的话像上面例子中给的一样,读文件、取一个对象出来强制成UserInfo的对象,然后再通过getUserName()和getUserPassword()你想要的值:========
FileInputStream istream = new FileInputStream("userInfo.data");
ObjectInputStream in = new ObjectInputStream(istream);
UserInfo info = (UserInfo) in.readObject();
String username_I_want = info.getUserName();
String password_I_want = info.getUserPassword();==========================
or visit my blog: toppi.blogchina.com