首页>代码>Java开发小工具(集合遍历导出等方法):针对任何项目文件夹,可以递归遍历文件夹中的任何后缀名的文件,提取其中的中文>/springboot-01-cache/src/main/java/com/atguigu/cache/config/export.java
package com.atguigu.cache.config; import jxl.Workbook; import jxl.write.WritableFont; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import java.io.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Pattern; public class export { private static List<File> list; public static String t; public static String k; private static int p = 0 ; private static List<Map<Integer ,String>>listMaps = new ArrayList<>(); //清空 public static void showDirectory(File file){ File[] filesX = file.listFiles(); for(File y:filesX){ System.out.println(y.getAbsolutePath()); if(y.isDirectory()){ showDirectory(y); } String[] temp = {y.getAbsolutePath()}; System.out.println(temp.length); for (int i = 0; i < temp.length; i++) { List<String> list = new ArrayList<>(); //清空 System.out.println(temp[i]); t = temp[i]; File dir = new File(t); File[] files = dir.listFiles(); // 该文件目录下文件全部放入数组 if (files != null) { for (int j = 0; j < files.length; j++) { String fileName = files[j].getName(); if (files[j].isDirectory()) { // 判断是文件还是文件夹 getFileList(); //迭代 } else if (fileName.endsWith("java")) { // 判断文件名是否以.html结尾 String strFileName = files[j].getAbsolutePath(); System.out.println(strFileName); list.add(strFileName); } else { System.out.println(); continue; } } System.out.println(list.size()); for (int n = 0; n < list.size(); n++) { Map map = new HashMap<Integer ,String>(); // System.out.println("======================="); System.out.println(list.get(n)); // listMaps.add(list.get(n));//文件名称放进集合 map.put(0,list.get(n));//文件名称放进集合 // map.put(1,""); // map.put(2,""); k = list.get(n); file = new File(k); InputStream input = null; try { input = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } StringBuffer buffer = new StringBuffer(); byte[] bytes = new byte[1024]; try { for (int b; (b = input.read(bytes)) != -1; ) { buffer.append(new String(bytes, 0, b, "UTF-8")); } } catch (IOException e) { e.printStackTrace(); } String s = buffer.toString(); String str = s.replaceAll("\\<\\!\\-\\-.*?\\-\\-\\>", ""); String aa = str.replaceAll("/\\*{1,2}[\\s\\S]*?\\*/",""); String a = aa.replaceAll("//.*", ""); // String reg = "\"[^\"]*\""; String regEx = "([\\u4e00-\\u9fa5]+)"; Pattern p = Pattern.compile(regEx); java.util.regex.Matcher m = p.matcher(a); System.out.print("提取出来的中文有:"); System.out.println(m.groupCount()); int c =2; while (m.find()) { c = c+1; System.out.print(m.group() + ","); System.out.println(c); map.put(c,m.group()); if(map.size()>2){ /*for (int x = 0; x<map.size() - 1;x++){ for (int d = map.size() - 1;d >x;d--){ if (map.get(d).equals(map.get(x))){ map.remove(d); } } }*/ } } listMaps.add(map); System.out.println(); } Map map1 = new HashMap<Integer ,String>(); listMaps.add(map1); // listAll.add(listMaps); excel(listMaps); } } } } public static void main(String[] args) { File file = new File("F:\\IDEAworkspace\\NEWwork\\card-api-task\\src\\main\\java"); showDirectory(file); getFileList(); } public static void getFileList() { } public static void excel(List<Map<Integer ,String>> listMaps) { p = p+1; String targetfile = "e:/out.xls " ; //输出的excel文件名 String worksheet = "List" ; //输出的excel文件工作表名 String[] title = {"国际化" }; //excel工作表的标题 WritableWorkbook workbook; try { //创建可写入的Excel工作薄,运行生成的文件在tomcat/bin下 //workbook = Workbook.createWorkbook(new File("output.xls")); System.out.println("begin" ); OutputStream os=new FileOutputStream(targetfile); workbook= Workbook.createWorkbook(os); WritableSheet sheet = workbook.createSheet(worksheet, 0 ); //添加第一个工作表 jxl.write.Label label; int rownum = 1; for(int i = 0;i<listMaps.size();i++){ for(int j = 0;j<listMaps.get(i).size();j++){ // label = new jxl.write.Label(0,i+1, listMaps.get(i).get(j));//第一个参数代表的是列,第二个参数代表行,第三个参数代表内容 if(j != 1 && j != 2){ label = new jxl.write.Label(0,rownum, listMaps.get(i).get(j));//第一个参数代表的是列,第二个参数代表行,第三个参数代表内容 sheet.addCell(label); rownum ++; } // sheet.setColumnView(2, 12); } // System.out.println("============================"); // System.out.println(listMaps.get(i)); // label = new jxl.write.Label(i,p, listMaps.get(i));//第一个参数代表的是列,第二个参数代表行,第三个参数代表内容 // sheet.addCell(label); // sheet.setColumnView(0, 80); // sheet.setColumnView(1, 10); } jxl.write.WritableFont wf = new jxl.write.WritableFont(WritableFont.TIMES, 10 , WritableFont.BOLD, true ); jxl.write.WritableCellFormat wcfF = new jxl.write.WritableCellFormat(wf); jxl.write.Label labelCF = new jxl.write.Label( 0 , 0 , "文件名" ); // jxl.write.Label labelCFF = new jxl.write.Label( 1 , 0 , "是否国际化" ); // jxl.write.Label labelCFFF = new jxl.write.Label( 2 , 0 , "完成度(%)" ); sheet.addCell(labelCF); // sheet.addCell(labelCFF); // sheet.addCell(labelCFFF); workbook.write(); workbook.close(); }catch (Exception e) { e.printStackTrace(); } System.out.println("end" ); Runtime r=Runtime.getRuntime(); Process p=null ; //String cmd[]={"notepad","exec.java"}; String cmd[]={"E:\\导出" , "out.xls" }; try { p=r.exec(cmd); } catch (Exception e){ System.out.println("error executing: " +cmd[ 0 ]); } } }