java - 如何更新.txt文件java中的内容

标签 java file text file-io io

<分区>

例如,我有一个名为 file.txt 的文件,其中包含个人信息(身份证、姓名、薪水):

A123 Anna 3000
A234 Alex 4000
A986 Jame 5000

如何编写允许用户输入个人 ID 并补充工资的 Java 代码? 最终输出将如下所示:

输入ID:A123

输入补薪:2000

运行程序后的文件.txt:

A123 Anna 5000
A234 Alex 4000
A986 Jame 5000

这是我到目前为止所做的,但没有奏效:

public static void addDalary() throws IOException {
        String ID, Nanme;
        double salary;


            Scanner console = new Scanner(System.in);
            System.out.print("Enter ID : ");
            String pID = console.nextLine();
            System.out.print("Enter replenish salary: ");
            replenish = console.nextInt();

            Scanner input = new Scanner(new File("file.txt"));
            while (input.hasNext()) {
                ID = input.next();
                Name = input.next();
                salary = input.nextDouble();

                if (ID == pID) {    
                    salary = salary + replenish;
                }
            }
            input.close();
        }

我对这些东西很陌生。任何帮助将不胜感激。

最佳答案

你必须使用逐行从原始文件中读取它的方法。搜索薪水并将其替换为新数字。将内容写入新的临时文件。删除原文件。最后将临时文件替换为原来的文件名。这是执行此操作的代码:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class FileContentUpdater {

    public static void main(String args[]) throws IOException {
        String ID, Name;
        double salary;
        int replenish;

        Scanner console = new Scanner(System.in);
        System.out.print("Enter ID : ");
        String pID = console.nextLine();
        System.out.print("Enter replenish salary: ");
        replenish = console.nextInt();

        File originalFile = new File("file.txt");
        BufferedReader br = new BufferedReader(new FileReader(originalFile));

        // Construct the new file that will later be renamed to the original
        // filename.
        File tempFile = new File("tempfile.txt");
        PrintWriter pw = new PrintWriter(new FileWriter(tempFile));

        String line = null;
        // Read from the original file and write to the new
        // unless content matches data to be removed.
        while ((line = br.readLine()) != null) {

            if (line.contains(pID)) {
                String strCurrentSalary = line.substring(line.lastIndexOf(" "), line.length());
                if (strCurrentSalary != null || !strCurrentSalary.trim().isEmpty()) {
                    int replenishedSalary = Integer.parseInt(strCurrentSalary.trim()) + replenish;
                    System.out.println("replenishedSalary : " + replenishedSalary);
                    line = line.substring(0,line.lastIndexOf(" ")) + replenishedSalary;
                }

            }
            pw.println(line);
            pw.flush();
        }
        pw.close();
        br.close();

        // Delete the original file
        if (!originalFile.delete()) {
            System.out.println("Could not delete file");
            return;
        }

        // Rename the new file to the filename the original file had.
        if (!tempFile.renameTo(originalFile))
            System.out.println("Could not rename file");

    }
}

关于java - 如何更新.txt文件java中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16383578/

相关文章:

java - iterable.forEach() 和 iterable.stream().forEach() 的区别

java - 如何检查新的 JSON 文件是否可用?

file - Java中的StringBuilder编码

jquery - 如何在 blueimp/jQuery-File-Upload 上设置动态 url

R:将文本字符串转换为命令

java - android.support.v7.internal.view.menu.ActionMenuItemView 无法转换为 android.widget.Button

java - 异步 Web 服务回调机制可以用于 Java 和 .NET 客户端吗?

.net - 在 C# dll 中缓存数据的最佳方法是什么?

git - 逐字的 git 修订

java - 在 Java 中读取和写入 .txt 文件