用Scanner替换文件中的一行

3

我尝试用用户输入替换文本文件中的某一行时遇到了些问题。每次尝试替换该行时,文本文件中的所有其他行都被删除了。是否有人可以帮助我解决这个问题?

     public static void removedata(String s) throws IOException {

    File f = new File("data.txt");
    File f1 = new File("data2.txt");
    BufferedReader input = new BufferedReader(new InputStreamReader(
            System.in));
    BufferedReader br = new BufferedReader(new FileReader(f));
    PrintWriter pr = new PrintWriter(f1);
    String line;

    while ((line = br.readLine()) != null) {
        if (line.contains(s)) {

            System.out
                    .println("I see you are trying to update some information... Shall I go ahead?");
            String go = input.readLine();
            if (go.equals("yes")) {
                System.out.println("Enter new Text :");
                String newText = input.readLine();
                line = newText;
                System.out.println("Thank you, Have a good Day!");
                break;
            }
            if (go.equals("no")) {

                System.out.println(line);
                System.out.println("Have a good day!");
                break;
            }
        }

        pr.println(line);
    }
    br.close();
    pr.close();
    input.close();
    Files.move(f1.toPath(), f.toPath(), StandardCopyOption.REPLACE_EXISTING);

}

这是我的主要内容。
public static void main(String args[]) throws ParseException, IOException {
    /* Initialization */


    String[] keywords = { "day", "month" };
    Scanner in = new Scanner(System.in);
    Scanner scanner = new Scanner(System.in);
    String input = null;

    System.out.println("Welcome");
    System.out.println("What would you like to know?");

    System.out.print("> ");
    input = scanner.nextLine().toLowerCase();           

    for (int i = 0; i < keywords.length; i++) {



          if (input.contains(keywords[i])) {

          removedata(keywords[i]);
          }
    }

   }

我的文本文件包含“今天是星期二”和“月份是三月”。假设用户输入“今天是星期三”,我想用新行替换旧行。有什么建议吗?


你不能直接替换文本文件中的一行。你必须重写整个文件。 - But I'm Not A Wrapper Class
你的答案已经在 https://dev59.com/bmIj5IYBdhLWcg3wpWlx 中给出了吗? - Shervin
你试过使用 replace() 吗?而不是像这样写:newText = line... 另外,我确定你需要先读取文件,然后再写入整个文件。 - freddiev4
2个回答

1
要替换文本文件中的文本,您需要有一个临时文件,在其中存储修改后的文本。我认为您实际上是通过使用ff1来完成这个过程的。但是,您在while循环内使用了break;,因此一旦替换并打印该行,循环就会停止。我认为您所要做的就是删除那个break;

0

你有两个选项

  1. 通常情况下,按照惯例读取文件直到找到所需行,一定要将所有行写入某个临时文件。将新行写入临时文件,然后继续操作。现在用新文件替换旧文件。

    private void updateFile(String lineToFind, String lineToUse, File f) throws IOException{
    File tempFile = new File(f.getAbsolutePath()+".tmp");
    try(BufferedReader reader = new BufferedReader(new FileReader(f));
    PrintWriter writer = new PrintWriter(new FileWriter(tempFile))) {
        String line;
        while ((line = reader.readLine()) != null) {
            if (line.equals(lineToFind)) {
                writer.println(lineToUse);
            } else {
                writer.println(line);
            }
        }
    }
    f.delete();
    tempFile.renameTo(f);
    }
    
  2. 使用RandomAccessFile来操作文件内容。这更加复杂,可能不值得努力,但是这是一个选择。


选项1是OP目前正在做的...或试图做的。 - Tom

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接