联行号文件的内容如下:
105588073429,,,105,105581000018,,,,5880,,,,中国建设银行股份有限公司顺德大良支行,,,,,,,,,,,,,, 105588073437,,,105,105581000018,,,,5880,,,,中国建设银行股份有限公司顺德锦湖支行,,,,,,,,,,,,,, //以下省略
银行文件的内容如下:
104|中国银行 105|中国建设银行 313|城市商业银行 314|农村商业银行 //以下省略
结果文件
中国建设银行,中国建设银行股份有限公司顺德大良支行 中国建设银行,中国建设银行股份有限公司顺德锦湖支行 //以下省略
package com.xjh.util; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.text.SimpleDateFormat; import java.util.Date; /** * 联行号的拆分 (2013-01-11) * @author xiejiaohui * */ public class LianHanghao { public static void main(String[] args) throws Exception { printNowDate(); File lianHangHao_file = new File("d:\\LianHangHao3.txt"); File bankCode_file = new File("d:\\BankCode.txt"); File result_file = new File("d:\\result.txt"); // BufferedReader lianHanghao = new BufferedReader(new FileReader("d:\\LianHangHao2.txt")); // BufferedReader lianHanghao = new BufferedReader(new InputStreamReader(new FileInputStream(lianHangHao_file),"GB2312")); BufferedReader lianHanghao = new BufferedReader(new InputStreamReader(new FileInputStream(lianHangHao_file),"GBK")); // FileWriter result_bw = new FileWriter(new File("d:\\result.txt")); // BufferedWriter result_bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(result_file), "utf-8")); // BufferedWriter result_bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(result_file), "GB2312")); BufferedWriter result_bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(result_file), "GBK")); String lianHanghao_line; String bankCode_line; try { while ((lianHanghao_line = lianHanghao.readLine()) != null) { if (!lianHanghao_line.contains(",")) { if (lianHanghao_line.length() > 0) { println("这行格式有问题: " + lianHanghao_line); } continue; } String[] l_h_h = lianHanghao_line.split(","); // BufferedReader bankCode = new BufferedReader(new InputStreamReader(new FileInputStream(bankCode_file),"utf-8")); // BufferedReader bankCode = new BufferedReader(new InputStreamReader(new FileInputStream(bankCode_file),"GB2312")); BufferedReader bankCode = new BufferedReader(new InputStreamReader(new FileInputStream(bankCode_file),"GBK")); while ((bankCode_line = bankCode.readLine()) != null) { String[] b_l = bankCode_line.split("\\|"); if (l_h_h[3].equals(b_l[0])) { result_bw.append(b_l[1]); break; } else { continue; } } result_bw.append(","); result_bw.append(l_h_h[12]); result_bw.append("\n"); bankCode.close(); } } finally { lianHanghao.close(); result_bw.close(); } printNowDate(); } public static void println(String input) { System.out.println(input); } public static void printNowDate() { println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); } }
这个程序需要读取两个文件,写入一个文件,我得出如下情况
如果读入的两个文件,保存的格式都是 ANSI/ASCII 则读取文件的InputStreamReader必须为GBK否则有乱码
如果读取的两个文件,保存的格式都是 UTF-8 则读取文件的InputStreamReader必须为utf-8否则有乱码
两个读入的文件,一个是 UTF-8 保存的,另外一个是 ANSI/ASCII 保存,只要读取文件的 InputStreamReader 格式跟文件保存格式保持一致,不会出现乱码。