import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
static File f2 = new File("测试文件1.txt");
static ArrayList<String> str = new ArrayList<>();
static ArrayList<String> reg = new ArrayList<>();
static ArrayList<String> strEnd = new ArrayList<>();
public static void main(String[] args) throws Exception {
double startTime = System.currentTimeMillis();
TestFile test = new TestFile();
str = test.findWord(f2);
reg = test.findWord(f);
String replaceStr = null;
Iterator it = str.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
for (int i = 0; i < str.size(); i++) {
replaceStr = str.get(i);
for (int j = 0; j < reg.size(); j++) {
replaceStr = replaceStr.replace(reg.get(j), "*");
}
strEnd.add(replaceStr);
}
System.out.println("等待写入到文件中...");
Iterator itStrEnd = strEnd.iterator();
String str = null;
while (itStrEnd.hasNext()) {
str = (String) itStrEnd.next();
test.writeFile(str);
}
System.out.println("总共所用时间:" + (System.currentTimeMillis() - startTime) / 1000+"秒");
}
}
class TestFile {
String str;
ArrayList<String> list = null;
int index = 0;
ArrayList<String> findWord(File f) throws Exception {
FileInputStream in = new FileInputStream(f);
InputStreamReader r = new InputStreamReader(in);
BufferedReader br = new BufferedReader(r);
list = new ArrayList<>();
while ((str = br.readLine()) != null) {
list.add(str);
}
return this.list;
}
void writeFile(String str) {
File file = new File("F:\\111\\比较文件.txt");
FileOutputStream os = null;
try {
os = new FileOutputStream(file, true);
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(str);
bw.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
public class FindFileTestFilter {
static File f = new File("敏感词库.txt");static File f2 = new File("测试文件1.txt");
static ArrayList<String> str = new ArrayList<>();
static ArrayList<String> reg = new ArrayList<>();
static ArrayList<String> strEnd = new ArrayList<>();
public static void main(String[] args) throws Exception {
double startTime = System.currentTimeMillis();
TestFile test = new TestFile();
str = test.findWord(f2);
reg = test.findWord(f);
String replaceStr = null;
Iterator it = str.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
for (int i = 0; i < str.size(); i++) {
replaceStr = str.get(i);
for (int j = 0; j < reg.size(); j++) {
replaceStr = replaceStr.replace(reg.get(j), "*");
}
strEnd.add(replaceStr);
}
System.out.println("等待写入到文件中...");
Iterator itStrEnd = strEnd.iterator();
String str = null;
while (itStrEnd.hasNext()) {
str = (String) itStrEnd.next();
test.writeFile(str);
}
System.out.println("总共所用时间:" + (System.currentTimeMillis() - startTime) / 1000+"秒");
}
}
class TestFile {
String str;
ArrayList<String> list = null;
int index = 0;
ArrayList<String> findWord(File f) throws Exception {
FileInputStream in = new FileInputStream(f);
InputStreamReader r = new InputStreamReader(in);
BufferedReader br = new BufferedReader(r);
list = new ArrayList<>();
while ((str = br.readLine()) != null) {
list.add(str);
}
return this.list;
}
void writeFile(String str) {
File file = new File("F:\\111\\比较文件.txt");
FileOutputStream os = null;
try {
os = new FileOutputStream(file, true);
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(str);
bw.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}