任务:读取本地java项目src文件夹下的所有.java文件,将内容合并到doc文件中
工具:可用org.apache.poi包HWPFdocument
public static void main(String[] args) throws IOException { // 被读取java文件总目录 String java_path = "D:\ntwxmngsys-vue\src"; // doc 文件目录 String doc_path = "D:\test.doc"; // 获取所有java文件 ListfileList = new ArrayList<>(); File file_d = new File(java_path); file(file_d,fileList); // 创建doc对象 File file = new File(doc_path); InputStream input = new FileInputStream(file); HWPFdocument worddocument = new HWPFdocument(input); FileOutputStream out = new FileOutputStream(file); Range range = worddocument.getRange(); // 获得java输入流 写入文件 for (File file_temp : fileList){ FileInputStream fi = new FileInputStream(file_temp); BufferedReader bi = new BufferedReader(new InputStreamReader(fi)); String line = ""; CharacterRun characterRun = range.insertAfter(""); characterRun.setBold(true); characterRun.setColor(111); range.insertAfter("r"); range.insertAfter("r"); while((line = bi.readLine())!=null){ range.insertAfter(line); range.insertAfter("r"); } // 每个文件加换行 range.insertAfter("r"); range.insertAfter("r"); bi.close(); fi.close(); } worddocument.write(out); // 关流 input.close(); out.close(); } private static void file(File file_d , List fileList) { File[] files1 = file_d.listFiles(); for (File temp_file :files1){ if (temp_file.isDirectory()) { file(temp_file,fileList); }else { if(temp_file.getName().lastIndexOf(".vue") != -1){ fileList.add(temp_file); } } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)