當前位置:成語大全網 - 新華字典 - JAVA合並兩個文件並去重

JAVA合並兩個文件並去重

先做壹個buffer:

StringBuilder sb_a = new StringBuilder(); // for a

StringBuilder sb_b = new StringBuilder(); // for b

然後讀文件:

Path aFile = Paths.get("a.txt");

try (BufferedReader reader = Files.newBufferedReader( aFile, Charset.defaultCharset())) {

String lineFromFile = "";

while((lineFromFile = reader.readLine()) != null){

sb_a.append(lineFromFile);

}

} catch(IOException exception) {

System.out.println("Error while reading file");

}

b.txt也是同壹個方法

跟著以妳自定義的方法去比較兩個怎樣合拼,暫存為String merged = ...;

最後寫回文件:

Path abFile = Paths.get("ab.txt");

try (BufferedWriter writer = Files.newBufferedWriter( abFile, Charset.defaultCharset())) {

writer.append(merged);

writer.newLine(); // optional

writer.flush();

} catch(IOException exception) {

System.out.println("Error writing to file");

}