【发布时间】:2013-01-06 09:24:09
【问题描述】:
我正在逐行读取文件,并且我正在努力做到这一点,以便如果我到达适合我的特定参数的行(在我的情况下,如果它以某个单词开头),我可以覆盖那条线。
我当前的代码:
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 对象。
与往常一样,我们非常感谢任何帮助、示例或建议。
谢谢!
【问题讨论】:
-
为什么不一次读入整个文件,关闭它,打开它进行写入,然后在对其执行转换后写出每一行?
-
@Patashu 如果它是一个大文件怎么办?那么我提出的方法会更好。
-
请不要使用DataInputStream读取文本vanillajava.blogspot.co.uk/2012/08/…
标签: java file file-io overwrite read-write