java 修改txt文件中某一行的内容

[size=x-large]对文档修改某一行,需要读进来,修改,然后再写进去。最快的方法就是边读边写。[/size]
import java.io.*;
public class Modifytxt{
public static void main(String args[])throws IOException{
int modifyline=1;//要修改的行
BufferedReader in=new BufferedReader(new FileReader("1.java"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("2.java")));
String line;
int count=1;
while((line=in.readLine())!=null){
if(count==modifyLine){
out.println(line.replace('abc', 'def')); //替换abc成def
}else{
out.println(line);
}
count++;
}
in.close();
out.close();
}
}

你可能感兴趣的:(Java,IO)