我正在尝试更改 txt 文件中的部分行。它正在工作,但是如果文件中的行包含特殊字符,例如?!(它不工作有什么问题?
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class nomarks
{
public static void main(String[] args)
{
FileInputStream fstream;
try
{
fstream = new FileInputStream("readme.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String oldText = "i have !? in my text";
String textToChange = "cant solve the problem";
String line;
String holeFile = "";
while ((line = br.readLine()) !=null)
{
holeFile += line + "\r\n";
}
br.close();
FileWriter writer = new FileWriter("readme.txt");
String newtext = holeFile.replaceAll(oldText, textToChange);
writer.write(newtext);
writer.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}