//因爲要暴力破解密碼,今天就下載了n多個字典,因爲每次要一個一個的搞有點麻煩,因此想把全部的字典中的內容重新寫入到一個新的字典中,不羅嗦了,直接貼代碼,但願能對你們有所幫助,因爲是小白,代碼質量差,勿見怪 package com.sam; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class MergeDictionary { // 得到路徑 文件路徑我保存在src下filePath.properties中 public String getFilePath(String propertiesName) { Properties prop = new Properties(); InputStream in = this.getClass().getClassLoader() .getResourceAsStream("filePath.properties"); try { prop.load(in); } catch (IOException e) { e.printStackTrace(); } return prop.getProperty(propertiesName).trim(); } //將老文件的內容寫入新文件 public void mergeDictionary(String oldPath, String writeFileName) { try { BufferedReader br = new BufferedReader(new FileReader(new File( oldPath))); String line = null; BufferedWriter bw = new BufferedWriter(new FileWriter( writeFileName, true)); bw.write("\n"); while ((line = br.readLine()) != null) { bw.write(line + "\n"); } bw.flush(); bw.close(); br.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { String oldPath = new MergeDictionary().getFilePath("oldPath"); String writeFileName = new MergeDictionary() .getFilePath("writeFileName"); File filePath = new File(oldPath); File[] childs = filePath.listFiles(); for (int i = 0; i < childs.length; i++) { if (childs[i].getName().equals("dictionary.txt")) {//由於我寫的oldpath和新的路徑是同樣,因此要判斷一下 } else { new MergeDictionary().mergeDictionary( childs[i].getAbsolutePath(), writeFileName); } } System.out.println("successful!"); } }