我正在逐行读取文件,并尝试使得当我到达符合我特定参数的行时(在我的情况下如果它以某个特定单词开头),我可以覆盖那一行。
当前代码:
try {
FileInputStream fis = new FileInputStream(myFile);
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
if (line.startsWith("word")) {
// replace line code here
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
...其中myFile
是一个File
对象。
和往常一样,任何帮助、示例或建议都将不胜感激。
谢谢!