简繁字体转换(简繁字体转换器手机版)

更新时间:2023-03-02 12:50:26 阅读: 评论:0

一:需求分析

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

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

二:代码示例

package com.java_study;import com.spreada.utils.chine.ZHConverter;import java.io.*;import java.util.ArrayList;import java.util.regex.Matcher;import java.util.regex.Pattern;/*** Created by dd on 2022/6/8.*/public class ChangeSimpleToTraditional {public static void changeFileFromSimpleChineToTradionalWithRootPath(String path){ArrayList<String> tempArray = new ArrayList<String>();ArrayList<String> fileList = traverFolder2(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 = Pattern.compile("[u4e00-u9fcc]+");if (pattern.matcher(temp).find()){temp = getChine(temp);}strBuffer.append(temp);strBuffer.append(System.getProperty("line.parator"));}System.out.println(strBuffer.toString());bufRead.clo();PrintWriter printWriter = new PrintWriter(filePath);printWriter.write(strBuffer.toString().toCharArray());printWriter.flush();printWriter.clo();}catch (IOException e){e.printStackTrace();}}

/**把读取的文件的每一行字符串进行正则匹配简体中文

* 并且把匹配到的简体中文替换为繁体

* 并返回替换后的字符串

* paramValue:读文件时候,读取到的每一行字符串*/

public static String getChine(String paramValue) {String regex = "([u4e00-u9fa5]+)";String replacedStr = paramValue;Matcher matcher = Pattern.compile(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> traverFolder2(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;} el {for (File file2 : files) {if (file2.isDirectory()) {System.out.println("文件夹:" + file2.getAbsolutePath());traverFolder2(file2.getAbsolutePath(),listFileName);} el {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());}}}} el {System.out.println("文件不存在!");}return listFileName;}}

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

本文发布于:2023-02-28 21:10:00,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/zhishi/a/1677732626105541.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:简繁字体转换(简繁字体转换器手机版).doc

本文 PDF 下载地址:简繁字体转换(简繁字体转换器手机版).pdf

标签:简繁   转换器   字体   字体转换   手机
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 实用文体写作网旗下知识大全大全栏目是一个全百科类宝库! 优秀范文|法律文书|专利查询|