比如我有一个txt文件里有一行“0001 你好,明天,呵呵”,要求从控制台输入0001后把文件中整行删除
import java.io.*;
import java.util.Scanner;
public class FileFilter {
public static void main(String[] args)throws IOException{
System.out.print("输入字符串:");
String str=(new Scanner(System.in)).next();
String oldFile="1.dat";
String newFile="1_.dat";
BufferedReader in=new BufferedReader(new InputStreamReader( new FileInputStream(oldFile), "utf-8"));
BufferedWriter ou=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFile),"utf-8"));
String line;
while( (line=in.readLine())!=null){
if(line.indexOf(str)<0){
ou.write(line);
ou.newLine();
}
}
in.close();
ou.close();
}
}
import java.util.Scanner;
public class FileFilter {
public static void main(String[] args)throws IOException{
System.out.print("输入字符串:");
String str=(new Scanner(System.in)).next();
String oldFile="1.dat";
String newFile="1_.dat";
BufferedReader in=new BufferedReader(new InputStreamReader( new FileInputStream(oldFile), "utf-8"));
BufferedWriter ou=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFile),"utf-8"));
String line;
while( (line=in.readLine())!=null){
if(line.indexOf(str)<0){
ou.write(line);
ou.newLine();
}
}
in.close();
ou.close();
}
}
温馨提示:答案为网友推荐,仅供参考