JAVA小项目-银行管理系统(图形化界面)5-用户信息读写

这里写图片描述

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; i<n; i++)
            {
                if(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<n; 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<n; 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

为了实现Java学生管理系统的输入输出流读写,可以使用Java标准库中的FileInputStream/FileOutputStream和ObjectInputStream/ObjectOutputStream类。 当需要保存学生信息时,可以将学生对象序列化为字节流后写入到文件中,代码示例: ```java try { FileOutputStream fos = new FileOutputStream("students.dat"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(student); // student为学生对象 oos.close(); } catch (IOException e) { e.printStackTrace(); } ``` 当需要读取学生信息时,可以从文件中读取字节流并反序列化为学生对象,代码示例: ```java try { FileInputStream fis = new FileInputStream("students.dat"); ObjectInputStream ois = new ObjectInputStream(fis); Student student = (Student) ois.readObject(); // Student为学生类 ois.close(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } ``` 为了实现图形化界面,可以使用Java Swing或JavaFX等GUI库。在界面上添加按钮或菜单项,通过事件监听器来响应用户操作,更新学生信息并进行读写操作。例如,当用户点击保存按钮时,程序将当前学生对象序列化并写入到文件中;当用户点击读取按钮时,程序从文件中读取学生对象并在界面上展示。 代码示例: ```java // 创建保存按钮 JButton saveButton = new JButton("保存"); saveButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { FileOutputStream fos = new FileOutputStream("students.dat"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(student); // student为当前学生对象 oos.close(); } catch (IOException ex) { ex.printStackTrace(); } } }); // 创建读取按钮 JButton loadButton = new JButton("读取"); loadButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { FileInputStream fis = new FileInputStream("students.dat"); ObjectInputStream ois = new ObjectInputStream(fis); Student student = (Student) ois.readObject(); // Student为学生类 ois.close(); // 在界面上展示学生信息 showStudentInfo(student); } catch (IOException | ClassNotFoundException ex) { ex.printStackTrace(); } } }); ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值