こんにちは、詩を含むファイルを読み取るコードを作成しようとしています。次に、すべての行の最初の「you」を「we」に変更します。replaceFirst()、replace()、replaceAll(); を使用しようとしています。ただし、何かを置き換えるのに機能したものは1つもありません。
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");
}
}