java中按名称更新txt文件中两列的记录

发布于 2025-01-19 00:17:05 字数 955 浏览 0 评论 0原文

我有员工的txt文件 它包含名称,日期,帖子,薪水:

Asher 2022/04/05 HR-Manager 24000

我更改了我的代码,这效果很好(也许对某人有帮助):

Scanner sc = new Scanner(System.in);
        System.out.println("•\tWrite name of employee to change salary and post");
        String name = sc.nextLine();
        System.out.println("•\tNew post:");
        String post = sc.nextLine();
        System.out.println("•\tNew salary:");
        String salary = sc.nextLine();
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new FileReader(oldFileName));
            bw = new BufferedWriter(new FileWriter(tmpFileName));
            String line;
            while ((line = br.readLine()) != null) {
            String[] arr =line.split(" ");
                if (line.contains(name))
                    line = line.replace(arr[2], post);
                bw.write(line+"\n");
            }

I have txt file with employees
It contains name, date, post, salary:

Asher 2022/04/05 HR-Manager 24000

I've changed my code, this one works fine (maybe would be helpfull for someone):

Scanner sc = new Scanner(System.in);
        System.out.println("•\tWrite name of employee to change salary and post");
        String name = sc.nextLine();
        System.out.println("•\tNew post:");
        String post = sc.nextLine();
        System.out.println("•\tNew salary:");
        String salary = sc.nextLine();
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new FileReader(oldFileName));
            bw = new BufferedWriter(new FileWriter(tmpFileName));
            String line;
            while ((line = br.readLine()) != null) {
            String[] arr =line.split(" ");
                if (line.contains(name))
                    line = line.replace(arr[2], post);
                bw.write(line+"\n");
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文