UserMessage这个类 应该是 我在做这个最难的一块东西了, 文件读写。
虽然细节并没有处理好。 用数据库写 应该简单一点。
他分为4部分组成
1.将注册的信息格式化写入文本
2.读取信息,将用户名信息返回(如果不存在返回null),和Check类配合使用
3.在存款,取款和转账操作 时 更新金额
4.修改密码时更新密码
先给出该类的框架、
public class UserMessage{
}
1.将注册的信息格式化写入文本
首先要看下 文件应该怎么写入吧 创建StringBuffer 对象去存储文本信息, 然后写入
public void write(String[] message)throws IOException
{
File file=new File("Message.txt"); //创建文件对象
String messagesum="";
for (int i=0; i<5; i++) //将信息格式化存储
messagesum+=message[i]+"~";
if(!file.exists())
file.createNewFile();
FileOutputStream out=new FileOutputStream(file,true); //建立输出对象,true表示追加
StringBuffer sb=new StringBuffer(); //创建字符串流
sb.append(messagesum+"\n"); //向字符串流中添加信息
out.write(sb.toString().getBytes("gb2312")); //将字符串流中的信息写入文本
out.close(); //关闭
}
2.读取信息,将用户名信息返回(如果不存在返回null),和Check类配合使用
/*
* 读取信息,将用户名信息返回(如果不存在返回null),和Check类配合使用
*/
public String[] read(String countname) throws IOException
{
File file=new File("Message.txt");
if(!file.exists()||file.isDirectory())
throw new FileNotFoundException();
BufferedReader br=new BufferedReader(new FileReader(file));
String temp=null;
StringBuffer sb=new StringBuffer();
temp=br.readLine();
String []message = new String[5]; //按~拆分 成5个字符串数组,按账号和密码进行信息验证
while(temp!=null){
String sbstring = temp.toString();
int n = sbstring.length(); //测字符串长度
for (int i=0; i<5; i++)
message[i] = "";
int k=0;
for (int i=0; iif(sbstring.charAt(i)=='~')
{
//System.out.println("@"+message[k]);
k++;
}
else
{
message[k] += sbstring.charAt(i);
}
}
if (message[2].equals(countname)) //返回找到用户的信息
{
return message;
}
temp=br.readLine();
}
return null;
}
3.在存款,取款和转账操作 时 更新金额
更新金额 就需要说一下了
思路就是 按行读入, 如果需要修改 修改后在加入BufferString的对象中
如果不需要修改直接加入BufferString的对象中 , 最后覆盖写入文件
我也不知道为什么用原文件对象,会写入失败,重新创建文件对象 就写入成功
/*在存款取款操作 时 更新金额
*
*/
public String updatemoney(String countname,int wangsave) throws IOException
{
File file=new File("Message.txt");
if(!file.exists()||file.isDirectory())
throw new FileNotFoundException();
//读文件
BufferedReader br=new BufferedReader(new FileReader(file));
String temp=null;
StringBuffer sb=new StringBuffer(); //建立字符串流
StringBuffer sb1=new StringBuffer();
String moneystring="";
temp=br.readLine();
String []message = new String[5]; //按~拆分 成5个字符串数组,按账号和密码进行信息验证
while(temp!=null){
String sbstring = temp.toString();
int n = sbstring.length(); //测字符串长度
for (int i=0; i<5; i++)
message[i] = "";
int k=0;
for (int i=0; i//拆乘5个String
{
if(sbstring.charAt(i)=='~')
{
//System.out.println("@"+message[k]);
k++;
}
else
{
message[k] += sbstring.charAt(i);
}
}
if (message[2].equals(countname)) //找到该账户名
{
String newmessage="";
int moneyint;
moneyint=Integer.parseInt(message[4])+wangsave; //金额转为int操作
//原金额 //存入金额
if (moneyint<0)
{
return "负数";
}
moneystring = String.valueOf(moneyint); //将String转int
for (int i=0; i<4; i++) //转化为规定格式文件
newmessage += message[i]+"~";
newmessage += moneystring+"~";
sb1.append(newmessage+"\n");
}
else
{
sb1.append(temp+"\n");
}
temp=br.readLine();
}
/*
* 说明:
* 本来的想法是在原文件对象中覆盖内容,但是发现覆盖后文本为空, 无法解决
* 但重新创建文件对象,则可以完成操作
*/
File file1=new File("Message.txt"); //重新建立文件对象, 覆盖写入文本
if(!file1.exists())
file1.createNewFile();
FileOutputStream out=new FileOutputStream(file1,false); //false为重写操作
out.write(sb1.toString().getBytes("gb2312"));
out.close();
return moneystring;
}
4.修改密码时更新密码
其实更新密码和更新余额一样, 只是针对的文本信息变了
直接上代码
//更新密码
public String updatepwd(String countname,String pwd) throws IOException
{
File file=new File("Message.txt");
if(!file.exists()||file.isDirectory())
throw new FileNotFoundException();
//读文件
BufferedReader br=new BufferedReader(new FileReader(file));
String temp=null;
StringBuffer sb=new StringBuffer();
//写文件
//FileOutputStream out=new FileOutputStream(file,false);
StringBuffer sb1=new StringBuffer();
String moneystring="";
temp=br.readLine();
String []message = new String[5]; //按~拆分 成5个字符串数组,按账号和密码进行信息验证
while(temp!=null){
String sbstring = temp.toString();
int n = sbstring.length(); //测字符串长度
for (int i=0; i<5; i++)
message[i] = "";
int k=0;
for (int i=0; i//拆乘5个String
{
if(sbstring.charAt(i)=='~')
{
//System.out.println("@"+message[k]);
k++;
}
else
{
message[k] += sbstring.charAt(i);
}
}
if (message[2].equals(countname)) //找到该账户名
{
//修改密码
for (int i=0; i<3; i++)
sb1.append(message[i]+"~");
sb1.append(pwd+"~");
sb1.append(message[4]+"~\n");
}
else
{
sb1.append(temp+"\n");
}
temp=br.readLine();
}
/*
* 说明:
* 本来的想法是在原文件对象中覆盖内容,但是发现覆盖后文本为空, 无法解决
* 但重新创建文件对象,则可以完成操作
*/
File file1=new File("Message.txt");
if(!file1.exists())
file1.createNewFile();
FileOutputStream out=new FileOutputStream(file1,false);
out.write(sb1.toString().getBytes("gb2312"));
out.close();
return moneystring;
}
JAVA小项目-银行管理系统(图形化界面)1-菜单
http://blog.csdn.net/changjiale110/article/details/78880024
JAVA小项目-银行管理系统(图形化界面)2-开户与挂失
http://blog.csdn.net/changjiale110/article/details/78896168
JAVA小项目-银行管理系统(图形化界面)3-登录与查询
http://blog.csdn.net/changjiale110/article/details/78916391
JAVA小项目-银行管理系统(图形化界面)4-验证
http://blog.csdn.net/changjiale110/article/details/78916497
JAVA小项目-银行管理系统(图形化界面)5-用户信息读写
http://blog.csdn.net/changjiale110/article/details/78926473
JAVA小项目-银行管理系统(图形化界面)6-存款与取款
http://blog.csdn.net/changjiale110/article/details/78926600
JAVA小项目-银行管理系统(图形化界面)7-改密与转账
http://blog.csdn.net/changjiale110/article/details/78955018
整体项目演示+源码包
http://blog.csdn.net/changjiale110/article/details/78955353