我的完整代码package file;import java.io.*;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));
replace的内容在变吧
debug看下
我的完整代码package file;import java.io.*;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(); }
}
String str2 = "";
String str3 = "";
String str4 = "";str3 替换后应该放回 str3 才对,为什么要放到 str4 里面去呢?这样 str3 里面永远是不会变的,而且 str4 也是一直在变(因为在循环里面),也就是说 str4 的值是最后一次被替换的值,前面的替换等于白做。如果要替换 str3 的话,可以改成:str3 = str3.replace(str3.charAt(l), str2.charAt(i));试试看。另:建议不要使用字母“l”作为变量名,容易与数字“1”产生混淆。
但是和现在的str4所产生的是同样的结果。
得出的结果是没有替换的字符串
而我在什么都没变的情况下,调试了一遍
就变成了替换后的结果,真是搞不明白,调试到底是什么作用