什么东西?怎么回事? 2016-05-06 13:59 采纳率: 0%
浏览 2073
已结题

java在txt里怎么更新数据啊

这是一个模拟atm的 一行三个值 分别是账户 密码 和余额
我可以从txt里读出数据 就是不知道取了钱、存了钱以后我该怎么更新txt里的数据

 package atm;

/**
 * Created by lenovo on 2016/5/5.
 */
import java.awt.List;

import java.io.*;
import java.util.HashMap;
import java.util.Scanner;

public class c {
    public static void readTxtFile(String filePath){
        try {
            String encoding="GBK";
            File file=new File(filePath);


            if(file.isFile() && file.exists()){ //判断文件是否存在
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file),encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                int line=1;
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入账户");

                int a=sc.nextInt();
                HashMap m=new HashMap();
                while((lineTxt = bufferedReader.readLine())!= null){
                    String[] lin=lineTxt.split(" ");
                    m.put(lin[0]+lin[1],lin[2]);
                    int zh=Integer.parseInt(lin[0]);
                    int mm=Integer.parseInt(lin[1]);
                    if(a==zh){
                        System.out.println("请输入密码");
                        int b=sc.nextInt();
                        if(b==mm){


                            int g=0;
                            while(g<4) {
                                int money = Integer.parseInt(lin[2]);
                                System.out.println("1.查看余额\n2.取款\n3.存款\n4.退出\n");

                                int c=sc.nextInt();
                                {
                                    switch (c) {
                                        case 1:
                                            System.out.println("您账户内余额为:" + money);
                                            g = 1;
                                            break;
                                        case 2:
                                            System.out.println("输入您要取的金额:");
                                            int d = sc.nextInt();
                                            if (d > money) {
                                                System.out.println("余额不足!");
                                                g = 2;
                                            } else {
                                                money -= d;
                                                System.out.println("您账户内余额为:" + money);
                                                g = 2;
                                            }
                                            break;

                                        case 3:
                                            System.out.println("请输入你要存的金额:");
                                            int e = sc.nextInt();
                                            money += e;
                                            System.out.println("您账户内余额为:" + money);
                                            g = 3;
                                            break;
                                        case 4:break;
                                        default:
                                            System.out.println("1.查看余额\n2.取款\n3.存款\n4.退出\n");
                                            g = sc.nextInt();
                                            return;
                                    }


                                }

                            }
                        }
                        else
                        {
                            System.out.println("您输入的密码错误");
                        }
                    }else{
                        System.out.println("您输入的账户不存在");
                    }
                }
                read.close();
            }else{
                System.out.println("找不到指定的文件");
            }
        }
        catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }

    }


    public static void main(String arg[]){
        String filePath = "C:\\Users\\lenovo\\Desktop\\11.txt";

        readTxtFile(filePath);
    }



}


  • 写回答

3条回答 默认 最新

  • danielinbiti 2016-05-06 14:50
    关注
     public class c {
        public static void readTxtFile(String filePath){
            try {
                String encoding="GBK";
                File file=new File(filePath);
    
    
                if(file.isFile() && file.exists()){ //判断文件是否存在
                    InputStreamReader read = new InputStreamReader(
                            new FileInputStream(file),encoding);//考虑到编码格式
                    BufferedReader bufferedReader = new BufferedReader(read);
                    String lineTxt = null;
                    int line=1;
                    System.out.println("请输入账户");
                    Scanner sc = new Scanner(System.in);
    
                    int a=sc.nextInt();
                    HashMap m=new HashMap();
    
                    ArrayList<String> lineList = new ArrayList();
                    boolean isOper = false;//是否做存款取款动作了,钱没变化就没必要写文件了
                    boolean isLogined = false;//是否已经登录过,登陆过就不用再循环找是否还有相同用户了
                    while((lineTxt = bufferedReader.readLine())!= null){//先把文件读出来
                        lineList.add(lineTxt);
                    }
                    bufferedReader.close();
                    read.close();
                    for(int i=0;i<lineList.size()&&!isLogined;i++){
                        lineTxt = lineList.get(i);
                        String[] lin=lineTxt.split(" ");
                        m.put(lin[0]+lin[1],lin[2]);
                        int zh=Integer.parseInt(lin[0]);
                        int mm=Integer.parseInt(lin[1]);
                        if(a==zh){
                            System.out.println("请输入密码");
                            int b=sc.nextInt();
                            if(b==mm){
                                isLogined = true;
                                int g=0;
                                while(g<4) {
                                    int money = Integer.parseInt(lin[2]);
                                    System.out.println("1.查看余额\n2.取款\n3.存款\n4.退出\n");
    
                                    g=sc.nextInt();
                                    {
                                        switch (g) {
                                            case 1:
                                                System.out.println("您账户内余额为:" + money);
                                                break;
                                            case 2:
                                                System.out.println("输入您要取的金额:");
                                                int d = sc.nextInt();
                                                if (d > money) {
                                                    System.out.println("余额不足!");
                                                } else {
                                                    money -= d;
                                                    System.out.println("您账户内余额为:" + money);
                                                }
                                                lin[2] = money+"";//这里需要重新赋值
                                                isOper = true;
                                                break;
    
                                            case 3:
                                                System.out.println("请输入你要存的金额:");
                                                int e = sc.nextInt();
                                                money += e;
                                                System.out.println("您账户内余额为:" + money);
                                                lin[2] = money+"";//这里需要重新赋值
                                                isOper = true;
                                                break;
                                            case 4:
                                                lineTxt = lin[0] + " " +  lin[1] + " " +  lin[2];
                                                lineList.set(i, lineTxt);//更新余额
                                                break;
                                            default:
                                                System.out.println("1.查看余额\n2.取款\n3.存款\n4.退出\n");
                                                g = sc.nextInt();
                                        }
    
    
                                    }
    
                                }
                            }
                            else
                            {
                                System.out.println("您输入的密码错误");
                            }
                        }else{
                            System.out.println("您输入的账户不存在");
                        }
                    }
                    if(isOper){//写文件
                        FileWriter fw = new FileWriter(file);
                        //遍历clist集合写入到fileName中
                        for (String str: lineList){
                            fw.write(str);
                            fw.write("\n");
                        }
                        //刷新缓冲区
                        fw.flush();
                        //关闭文件流对象
                        fw.close();
                    }
                }else{
                    System.out.println("找不到指定的文件");
                }
            }
            catch (Exception e) {
                System.out.println("读取文件内容出错");
                e.printStackTrace();
            }
    
        }
    
    
        public static void main(String arg[]){
            String filePath = "C:\\11.txt";
    
            readTxtFile(filePath);
        }
    
    
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog