1、标准“读”、“写”
br.readLine() 会报IOException 错误, 直接throws IOException 即可,在main函数中,也可以不写try...catch,类似test, throws IOException即可。
public static void test(BufferedReader br,BufferedReader br2, PrintWriter writer) throws IOException {
String line = null;
String line2 = null;
int js =0;
Set<String> zTitle = new HashSet<String>();
while ((line = br.readLine()) != null) {
String array[] = line.split("\t", -1);
zTitle.add(array[1]);
}
while((line2 = br2.readLine()) != null ) {
String array2[] = line2.split("\t", -1);
if (array2.length < 8) {
continue;
} else {
String hTitle = array2[1];
if (js == 1) {
for (String s : zTitle) {
if (hTitle.equals(s)) {
writer.println(line);
}
}
} else {
writer.println(line);
js = 1 ;
}
}
}
}
public static void main(String[] args) {
String inPath = "";
String midPath = "";
String endPath = "";
String encoding = "utf-8";
BufferedReader br = null;
BufferedReader br2 = null;
PrintWriter writer = null;
long startTime=System.currentTimeMillis(); //获取开始时间
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(inPath), encoding));
br2 = new BufferedReader(new InputStreamReader(new FileInputStream(midPath), encoding));
writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(endPath), encoding));
//调用test方法
test(br, br2, writer);
long endTime=System.currentTimeMillis(); //获取结束时间
System.out.println("用时: "+(endTime-startTime)+"ms");
} catch (FileNotFoundException e) {
System.out.println("The file does not exit or the path is incorrect!");
System.exit(-1);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (br2 != null) {
br.close();
}
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、更简单一点的“写”
FileWriter writer;
writer = new FileWriter("D:\\test.txt", true);
BufferedWriter bw = new BufferedWriter(writer);
bw.write(str2+"\n");
bw.close();
writer.close();