要把txt文件中指定的行的内容删掉,java代码怎么写?最好能举个简单的例子,谢谢!
1个回答
展开全部
先读取,删除后再写进去
int lineDel=1;
BufferedReader br=new BufferedReader(new FileReader( "c:/a.txt "));
StringBuffer sb=new StringBuffer(4096);
String temp=null;
int line=0;
while((temp=br.readLine())!=null){
line++;
if(line==lineDel) continue;
sb.append(temp).append( "\r\n ");
}
br.close();
BufferedWriter bw=new BufferedWriter(new FileWriter( "c:/a.txt "));
bw.write(sb.toString());
bw.close();
int lineDel=1;
BufferedReader br=new BufferedReader(new FileReader( "c:/a.txt "));
StringBuffer sb=new StringBuffer(4096);
String temp=null;
int line=0;
while((temp=br.readLine())!=null){
line++;
if(line==lineDel) continue;
sb.append(temp).append( "\r\n ");
}
br.close();
BufferedWriter bw=new BufferedWriter(new FileWriter( "c:/a.txt "));
bw.write(sb.toString());
bw.close();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询