package com.chipmunk.test; import java.io.BufferedReader; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; import net.sourceforge.pinyin4j.PinyinHelper; /** * 更改项目中的中文图片,中文文件等中文路径 * @author chipmunk * @date 2018年3月14日 下午6:40:15 */ public class ChineseToPinyin { public static String getPinyin(String t){ char[]cs = t.toCharArray(); StringBuffer sb = new StringBuffer(); for (char c : cs) { String[]s=PinyinHelper.toHanyuPinyinStringArray(c); if (s!=null) { String u = s[0]; if ("行".equals(c+"")) { u = s[1]; }else if ("重".equals(c+"")) { u = s[1]; } u = u.substring(0,u.length()-1); sb.append(u); } } return sb.toString(); } public static String toPinyin(String t){ char[]cs = t.toCharArray(); StringBuffer sb = new StringBuffer(); for (char c : cs) { String[]s=PinyinHelper.toHanyuPinyinStringArray(c); if (s!=null) { String u = s[0]; if ("行".equals(c+"")) { u = s[1]; }else if ("重".equals(c+"")) { u = s[1]; }else { if (u.startsWith("lu:")) { u = u.replaceFirst("lu:", "lv"); // u="lv"; } } u = u.substring(0,u.length()-1); sb.append(u); }else { sb.append(c); } } return sb.toString(); } public static boolean isChinese(String text) { String regex = "([\u4E00-\u9FA5])"; int count1 = text.length(); String text2 = text.replaceAll(regex, ""); int count2 = text2.length(); return count1>count2; } static Map<String, String> ZHONGWEN_PINYIN = new LinkedHashMap<String, String>(); public static void traverseDir(File dir){ File[]files = dir.listFiles(); for (File file : files) { if (file.isDirectory()) { traverseDir(file); }else{ String filename = file.getName(); if (isChinese(filename)) { //去掉空格 String filename2 = filename.replaceAll(" ", ""); String pinyin = toPinyin(filename2); ZHONGWEN_PINYIN.put(filename,pinyin); File file_new = new File(file.getParentFile(),pinyin); file.renameTo(file_new); } } } } public static List<String> readLine(InputStream in, String charset) throws Exception { InputStreamReader isr = new InputStreamReader(in, charset); BufferedReader br = new BufferedReader(isr); List<String> list = new ArrayList<String>(); String s; while ((s = br.readLine()) != null) { String t = StringUtil.toString(s, null); if (t != null) { list.add(t); } } br.close(); isr.close(); in.close(); return list; } public static List<String> readLine(File file, String charset) throws Exception { FileInputStream in = new FileInputStream(file); return readLine(in, charset); } public static void write(File file, String text, String charset) throws Exception { FileOutputStream out = new FileOutputStream(file); byte[] bytes = text.getBytes(charset);//GBK,"UTF-8" out.write(bytes); out.close(); out.flush(); } public static boolean find(String word,String text){ if (StringUtil.isBlank(word)) { return false; } if (StringUtil.isBlank(text)) { return false; } char[]w = word.toCharArray(); char[]t = text.toCharArray(); int size = w.length; // List<String> list = new ArrayList<String>(); for (int i = 0; i < t.length; i++) { if(w[0]==t[i]){ if(i+size-1<t.length){ boolean bool = true; for (int j = 1; j < size; j++) { bool&=(w[j]==t[i+j]); if(!bool){ break; } } if (bool) { return true; } } } } return false; } /** * 查找text中是否包含的list的元素,返回包含的元素 * @Date: 2013-6-27下午02:57:28 * @Description: Set<String> * @param list * @param text * @return */ public static Set<String> seek(Collection<String> col,String text){ Set<String> set = new HashSet<String>(); for (String word : col) { boolean bool = find(word, text); if(bool){ set.add(word); } } return set; } /** * 更改中文路径 * @param path_dir_image * @param path_dir_html */ public static void changeChinesePath(String path_dir_image,String path_dir_html) { // System.out.println(toPinyin("20李白(1)诗人.jpg")); File dir_img = new File(path_dir_image); traverseDir(dir_img); Set<String> keys = ZHONGWEN_PINYIN.keySet(); // for (String k : keys) { // System.out.println(k+"="+ZHONGWEN_PINYIN.get(k)); // } try { File dir = new File(path_dir_html); File[]files = dir.listFiles(new FileFilter() { @Override public boolean accept(File f) { if (f.isFile()&&f.getName().endsWith(".html")) { return true; }else { return false; } } }); for (File file : files) { System.out.println(file.getName()); StringBuffer sb = new StringBuffer(); List<String> htmlList = readLine(file, "UTF-8"); for (String s : htmlList) { Set<String> filenames = seek(keys, s); // for (String ff : filenames) { // System.out.println(ff); // } if (filenames.size()>0) { for (String ss : filenames) { //处理括弧(1) String sss = ss.replaceAll("\\(","\\\\\\(").replaceAll("\\)","\\\\\\)"); s=s.replaceAll(sss, ZHONGWEN_PINYIN.get(ss)); System.out.println(ss+"=="+ZHONGWEN_PINYIN.get(ss)); System.out.println(s); } } sb.append(s); sb.append("\n"); } write(file, sb.toString(), "UTF-8"); } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { String path_dir_html = "E:\\abc\\m\\pp"; String path_dir_image = "E:\\abc\\m\\pp\\img"; changeChinesePath(path_dir_image, path_dir_html); } }
<audio controls="controls" style="display: none;"></audio>