1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > Java使用程序ZHConverter把工程内所有文件的简体字转换为繁体字

Java使用程序ZHConverter把工程内所有文件的简体字转换为繁体字

时间:2022-07-23 02:44:41

相关推荐

Java使用程序ZHConverter把工程内所有文件的简体字转换为繁体字

一:需求分析

(1)由于公司项目在开发之初没有适配繁体语言,大概是没有考虑到有朝一日项目可以卖到台湾、香港、澳门吧。

(2)但是公司的项目确实卖到澳门了,于是客户要求把项目中的所有字体修改为繁体字,这就苦逼了,公司的项目沉淀了这么多年的代码,感觉到这是一项浩大的工程,但是老大只给了一周的时间要求修改完毕。于是考虑了一下看看这么繁琐的机械化的工作能不能由程序来完成,就各种谷歌百度,试了几种方法,没有成功,于是便手动修改,改了一天,几乎接近崩溃,一天下来累成狗,但是并没有修改多少。于是决定还是看看能不能用程序来搞定,最后整理出来下面的代码,使用Java语言编写,整个项目只用了几分钟就把所有简体字转换成了繁体字。整体思路是 读文件------>修改字体------------------>写文件。使用时只需要调用changeFileFromSimpleChineseToTradionalWithRootPath(String path)方法,并传入文件夹名即可。

二:代码示例

package com.java_study;import com.spreada.utils.chinese.ZHConverter;import java.io.*;import java.util.ArrayList;import java.util.regex.Matcher;import java.util.regex.Pattern;/*** Created by zyf on /8/8.*/public class ChangeSimpleToTraditional {public static void changeFileFromSimpleChineseToTradionalWithRootPath(String path){ArrayList<String> tempArray = new ArrayList<String>();ArrayList<String> fileList = traverseFolder2(path , tempArray);System.out.println("文件数组" + fileList);if (fileList.size()==0){return;};for (int i = 0; i<fileList.size() ; i++){readOldFileAndWriteNewFileWithFilePath(fileList.get(i));}}public static void readOldFileAndWriteNewFileWithFilePath(String filePath){//简体转繁体try{BufferedReader bufRead = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath))));StringBuffer strBuffer = new StringBuffer();for (String temp = null;(temp = bufRead.readLine())!= null;temp = null ){Pattern pattern = pile("[\u4e00-\u9fcc]+");if (pattern.matcher(temp).find()){temp = getChinese(temp);}strBuffer.append(temp);strBuffer.append(System.getProperty("line.separator"));}System.out.println(strBuffer.toString());bufRead.close();PrintWriter printWriter = new PrintWriter(filePath);printWriter.write(strBuffer.toString().toCharArray());printWriter.flush();printWriter.close();}catch (IOException e){e.printStackTrace();}}/**把读取的文件的每一行字符串进行正则匹配简体中文* 并且把匹配到的简体中文替换为繁体* 并返回替换后的字符串* paramValue:读文件时候,读取到的每一行字符串*/public static String getChinese(String paramValue) {String regex = "([\u4e00-\u9fa5]+)";String replacedStr = paramValue;Matcher matcher = pile(regex).matcher(paramValue);while (matcher.find()) {System.out.println("----------"+matcher.group(0));ZHConverter converter2 = ZHConverter.getInstance(ZHConverter.TRADITIONAL);String traditionalStr = converter2.convert(matcher.group(0));replacedStr = replacedStr.replace(matcher.group(0),traditionalStr);System.out.println("zyf" + traditionalStr + replacedStr);}return replacedStr;}/**迭代遍历传入的根文件夹,获取每一级文件夹的每个文件* 并把文件名称以字符串形式装在数组返回* path:根文件夹路径* listFileName:用于返回文件路径的数组,由于这个是迭代方法采用外部传入该数组 */public static ArrayList<String> traverseFolder2(String path , ArrayList<String> listFileName ) {File file = new File(path);if (file.exists()) {File[] files = file.listFiles();if (files.length == 0) {System.out.println("文件夹是空的!");return null;} else {for (File file2 : files) {if (file2.isDirectory()) {System.out.println("文件夹:" + file2.getAbsolutePath());traverseFolder2(file2.getAbsolutePath(),listFileName);} else {String sbsolutePath = file2.getAbsolutePath();if (sbsolutePath.endsWith(".jsp") || sbsolutePath.endsWith(".js") || sbsolutePath.endsWith(".html") || sbsolutePath.endsWith(".java") ){listFileName.add(file2.getAbsolutePath());}System.out.println("文件:" + file2.getAbsolutePath());}}}} else {System.out.println("文件不存在!");}return listFileName;}}

代码中使用了别人封装好的jar包ZHConverter,里边封装好了简体字转换成繁体字以及繁体字转换成简体字的方法。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。