Java 基本I/O文本替换
我正在使用下面的程序读取搜索查询(>100MB)上的巨大搜索引擎数据库转储,并删除不需要的表数据,这样我就只剩下关键字了,这样我就可以为我的一个类的趋势挖掘数据 以下是我目前掌握的情况:Java 基本I/O文本替换,java,io,Java,Io,我正在使用下面的程序读取搜索查询(>100MB)上的巨大搜索引擎数据库转储,并删除不需要的表数据,这样我就只剩下关键字了,这样我就可以为我的一个类的趋势挖掘数据 以下是我目前掌握的情况: import java.io.*; public class FileUtil { public static void main(String args[]) { try { FileInputStream fStream = new FileInputS
import java.io.*;
public class FileUtil {
public static void main(String args[]) {
try {
FileInputStream fStream = new FileInputStream("\\searches.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(fStream));
//PrintStream out = new PrintStream(new FileOutputStream("searchesEdited.txt"));
while (in.ready()) {
System.out.println(in.readLine());
String keyword = "foo"; // selected keyword to delete in txt file
//search for string
//delete the string
//write newly edited file to searchesEdited.txt
}
in.close();
} catch (IOException e) {
System.out.println("File input error");
}
}
}
这就像预期的那样工作,并将所有数据输出到控制台,所以我的方向是正确的。现在我只需要替换/删除传递的关键字。我研究了
replaceAll()
方法,但似乎无法正确实现它。任何帮助都将不胜感激。您可以使用String.replace()替换为空字符串,例如
"search for a keyword and delete this keyword".replace("keyword", "")
返回
"search for a and delete this"
有关如何读取和写入文本文件的简单教程,请参见
String line=in.readLine();
String keyword = "foo";
String newLine=line.replaceAll(keyword,"");
或
replaceAll方法使用正则表达式(而不是字符串值)。检查specs[link](,java.lang.String))这似乎有效,但在我编译并运行它之后,我输入的文本没有保存和/或删除关键字。我所做的只是在上面的3行中输入。想法?你需要创建一个文件并在其中写入结果(换行符)。
String keyword ="\\bfoo\\b"; //word boundary match
String newLine=line.replaceAll(keyword,"");