一:需求分析
(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,感謝您對本站的認可!
本文鏈接:http://www.newhan.cn/zhishi/a/1677732626105541.html
版權聲明:本站內容均來自互聯網,僅供演示用,請勿用于商業和其他非法用途。如果侵犯了您的權益請與我們聯系,我們將在24小時內刪除。
本文word下載地址:簡繁字體轉換(簡繁字體轉換器手機版).doc
本文 PDF 下載地址:簡繁字體轉換(簡繁字體轉換器手機版).pdf
| 留言與評論(共有 0 條評論) |