嗨,我正在尝试编写代码来读取包含一首诗的文件。然后它将每行中的第一个“你”更改为“我们”。我一直在尝试使用replaceFirst()、replace()、replaceAll();然而,没有一个人能够取代任何东西。
import java.io.*;
import java.util.Scanner;//imports
public class TextEditorTester
{
private static boolean line_change;
public static void main(String[] args) throws FileNotFoundException
{
String line = "";
File inFile = new File("OldPoem.txt");
Scanner in = new Scanner(inFile);
PrintWriter out = new PrintWriter("NewPoem.txt");
while(in.hasNextLine()){
line = in.nextLine();
line.replace("you", "we");
out.println(line);
}
out.close();
File newFile = new File("NewPoem.txt");
Scanner newOne = new Scanner(newFile);
System.out.println(newOne.nextLine());
System.out.println("Expected: Have we ever tried to enter the long black branches of other lives");
}
}