我有一个文本文件,其中包含数据库表数据,并试图删除表的一部分。这是该文件中:基本的java I/O文本替换,没有得到预期的输出
name= john
name= bill
name= tom
name= bob
name= mike
这里是编译和运行我的Java代码,但输出是不是我所期待和坚持。
import java.io.*;
public class FileUtil {
public static void main(String args[]) {
try {
FileInputStream fStream = new FileInputStream("\\test.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(fStream));
while (in.ready()) {
//System.out.println(in.readLine());
String line = in.readLine();
String keyword = "name="; //keyword to delete in txt file
String newLine=line.replaceAll(keyword,""); //delete lines that say name=
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("testEdited.txt"));
out.write(newLine.getBytes());
out.close();
}
in.close();
} catch (IOException e) {
System.out.println("File input error");
}
}
}
在testEdited文件的输出是这样的:
迈克
显然我想只剩下5名。谁能帮我吗? 感谢
您的'新的FileOutputStream'将始终写入文件开头的数据。考虑使用另外一个将append作为布尔参数的构造函数。或者在顶部打开一次,然后在程序结束时关闭一次。哦,这是功课吗? – 2012-01-31 02:07:56
不,我要创建这个程序来帮助一个不同类的数据挖掘项目,我需要删除不需要的表数据,并试图刷我的Java。 – user1170757 2012-01-31 02:10:39