Java 从扫描仪替换文件中的一行

Java 从扫描仪替换文件中的一行,java,Java,我在尝试用用户输入替换文本文件中的一行时遇到了一些问题。每当我尝试替换该行时,文本文件中的所有其他行都会被删除。有人能帮我解决这个问题吗 public static void removedata(String s) throws IOException { File f = new File("data.txt"); File f1 = new File("data2.txt"); BufferedReader input = new BufferedRea

我在尝试用用户输入替换文本文件中的一行时遇到了一些问题。每当我尝试替换该行时,文本文件中的所有其他行都会被删除。有人能帮我解决这个问题吗

     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]);
          }
    }

   }
publicstaticvoidmain(字符串args[])抛出ParseException,IOException{
/*初始化*/
字符串[]关键字={“天”,“月”};
扫描仪输入=新扫描仪(系统输入);
扫描仪=新的扫描仪(System.in);
字符串输入=null;
System.out.println(“欢迎”);
System.out.println(“您想知道什么?”;
系统输出打印(“>”);
输入=scanner.nextLine().toLowerCase();
对于(int i=0;i

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

要替换文本文件中的文本,您需要有一个临时文件来存储修改后的文本。我想你实际上是通过使用
f
f1
来做到这一点的
内,而
循环,因此,一旦更换并打印行,循环停止。我想你所要做的就是去掉
break

您有两个选择

  • 像往常一样读取文件,直到找到要查找的行,确保将所有行写入某个临时文件。将新行写入临时文件,然后继续操作。现在用新文件替换旧文件

    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);
    }
    
  • 使用命令来操作文件内容。这要复杂得多,可能不值得付出努力,但这是一种选择


  • 不能直接替换文本文件中的行。您已经重写了整个文件。您的答案已经在中给出了?您是否尝试过
    replace()
    ?另外,我确定您必须先读取文件,然后再写入整个文件选项1是OP当前正在执行的操作。。。或者试着去做。