日期:2014-05-20  浏览次数:20752 次

怎么用Java 的 IO 改写txt文件中第四行的数据,
帮我实现一下吧!我做不出来

------解决方案--------------------
for example
Java code
BufferedReader br = new BufferedReader(new FileReader("test.txt"));
PrintStream ps = new PrintStream(new FileOutputStream("test.txt.new"));
String buf;
int count = 0;
while ((buf=br.readLine()) != null) {
    count++;
    if (count == 4) {
        buf = buf.replaceAll("xxx", "yyy");
    }
    ps.println(buf);
}
br.close();
ps.close();
File f1 = new File("test.txt");
f1.delete();
File f2 = new File("test.txt.new");
f2.renameTo(f1);