//因为要暴力破解密码,今天就下载了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!"); } }