刚进公司没几天,经理给我两个utf-8文件,一个是用字母打头,繁体字内容的文件。另一个是简体和繁体对照的文件,说是让我编一个程序,根据第二个文件的简繁体对换,把第一个文件中的繁体全部变成简体。
当时真的是没有头绪,也不知道该从何处下手,查了很多资料,终于写了一个小程序,为了那些有这方面需要的朋友提供一些我力所能及的事。下面是我的代码,大家可以看看:
package file;
import java.io.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class Pro7 {
public static void readfile() {
try {
File file = new File("d://shu//trad-simp.utf-8");
File file1 = new File("d://shu//input-pinyin.utf-8");
InputStream input = new FileInputStream(file);
InputStream input1 = new FileInputStream(file1);
InputStreamReader inputreader =
new InputStreamReader(new FileInputStream(file), "UTF-8");
InputStreamReader inputreader1 =
new InputStreamReader(new FileInputStream(file1), "UTF-8");
BufferedReader buffer = new BufferedReader(inputreader);
BufferedReader buffer1 = new BufferedReader(inputreader1);
String str1 = new String();
String str2 = new String();
String str3 = new String();
String str4=new String();
char[] ch = new char[10000];
char[] ch1 = new char[5000];
char[] ch2 = new char[3000];
byte[] by = new byte[540000];
int j = 0;
int k = 0;
if (input1.read(by) != -1) {
str3 = new String(by, "utf-16");
}
if (buffer.read(ch) != -1) {
for (int i = 0; i < 10000; i = i + 4) {
ch1[j++] = ch[i];
}
for (int i = 2; i < 10000; i = i + 4) {
ch2[k++] = ch[i];
}
}
str1 = String.valueOf(ch1);
str2 = String.valueOf(ch2);
System.out.println(str1);
System.out.println(str2);
for (int i = 0; i < 5000; i++) {
for (int l = 0; l < 260000; l++) {
if (str1.charAt(i) == str3.charAt(l)) {
System.out.println("str3的第 " + l + " 个字符需要替换");
System.out.println(
str2.charAt(i) + "替换" + str3.charAt(l));
str4=str3.replace(str3.charAt(l), str2.charAt(i));
}
}
}
System.out.println(str4);
input.close();
input1.close();
inputreader.close();
inputreader1.close();
buffer.close();
buffer1.close();
} catch (IOException e) {
System.err.println(e);
}
}
public static void main(String[] args) {
readfile();
}
}