读写文件出现乱码

干饭人之路 2009-10-16 10:57:57
有这样一个文件
<record row="1">
<cell name="bank" column="S">F204H101500000001</cell>
<cell name="serianumber" column="T">GB02447521</cell>
<cell name="amount" column="U">500</cell>
<cell name="risk-state" column="V">1</cell>
<cell name="guarantor1-code" column="W">
</cell>
<cell name="guarantor1-name" column="X">
</cell>
<cell name="guarantor2-code" column="Y">
</cell>
<cell name="guarantor2-name" column="Z">
</cell>
<cell name="industry-code" column="AA">H63</cell>
<cell name="restricted-industry" column="AB">N</cell>
<cell name="expire-date" column="AC">20091224</cell>
</record>
<record row="2">
<cell name="bank" column="S">F204H101500000001</cell>
<cell name="serianumber" column="T">GB02447523</cell>
<cell name="amount" column="U">200</cell>
<cell name="risk-state" column="V">1</cell>
<cell name="guarantor1-code" column="W">
</cell>
<cell name="guarantor1-name" column="X">
</cell>
<cell name="guarantor2-code" column="Y">
</cell>
<cell name="guarantor2-name" column="Z">
</cell>
<cell name="industry-code" column="AA">H63</cell>
<cell name="restricted-industry" column="AB">N</cell>
<cell name="expire-date" column="AC">20091224</cell>
</record>
<record row="3">
<cell name="bank" column="S">F204H101500000001</cell>
<cell name="serianumber" column="T">GB02447515</cell>
<cell name="amount" column="U">400</cell>
<cell name="risk-state" column="V">1</cell>
<cell name="guarantor1-code" column="W">
</cell>
<cell name="guarantor1-name" column="X">
</cell>
<cell name="guarantor2-code" column="Y">
</cell>
<cell name="guarantor2-name" column="Z">
</cell>
<cell name="industry-code" column="AA">H63</cell>
<cell name="restricted-industry" column="AB">N</cell>
<cell name="expire-date" column="AC">20091222</cell>
</record>

我以行的形式读入内存,做处理后再写入新的文件中,但是出现乱码,为什么?
java程序如下:

import java.io.*;

/**
*
* @author Administrator
*/
public class ReadWriteStr_de {
public static final String UTF_8 = "UTF-8";

/**
* 字符串编码转换的实现方法
* @param str 待转换的字符串
* @param newCharset 目标编码
*/
public static String changeCharset(String str, String newCharset) throws UnsupportedEncodingException {
if(str != null) {
//用默认字符编码解码字符串。与系统相关,中文windows默认为GB2312
byte[] bs = str.getBytes();
return new String(bs, newCharset); //用新的字符编码生成字符串
}
return null;
}

/**
* 字符串编码转换的实现方法
* @param str 待转换的字符串
* @param oldCharset 源字符集
* @param newCharset 目标字符集
*/
public static String changeCharset(String str, String oldCharset, String newCharset) throws UnsupportedEncodingException {
if(str != null) {
//用源字符编码解码字符串
byte[] bs = str.getBytes(oldCharset);
return new String(bs, newCharset);
}
return null;
}


public static String toUTF_8(String str) throws UnsupportedEncodingException {
return ReadWriteStr_de.changeCharset(str, UTF_8);
}

public static int findstr(String str,String findstr){
int i=0;
if(str.indexOf(findstr)>0){
i=str.indexOf(findstr);
}
else {
i=str.length();
}
return i;
}
public static boolean startWithABC(String str){
boolean tf=false;
if(str.startsWith("A")||
str.startsWith("B")||
str.startsWith("C")||
str.startsWith("D")||
str.startsWith("E")||
str.startsWith("F")||
str.startsWith("G")||
str.startsWith("H")||
str.startsWith("I")||
str.startsWith("J")||
str.startsWith("K")||
str.startsWith("L")||
str.startsWith("M")||
str.startsWith("N")||
str.startsWith("O")||
str.startsWith("P")||
str.startsWith("Q")||
str.startsWith("R")||
str.startsWith("S")||
str.startsWith("T")||
str.startsWith("U")||
str.startsWith("V")||
str.startsWith("W")||
str.startsWith("X")||
str.startsWith("Y")||
str.startsWith("Z")
)
{
tf=true;
}

return tf;
}
public static void main(String[] args) throws Exception{
String filePath = "d:/javatest/temp/200910_de_其它支行汇总.xml";
String tmpFile = "d:/javatest/temp/200910_de_其它支行汇总_new.xml";
FileReader myFileR=new FileReader(filePath);
BufferedReader myBufferedReader=new BufferedReader(myFileR);
//FileWriter myFileW=new FileWriter(tmpFile);
//BufferedWriter myBufferedWriter=new BufferedWriter(myFileW);
FileWriter writer=new FileWriter(tmpFile);
BufferedWriter myBufferedWriter=new BufferedWriter(writer);
//System.out.println("test1");
String[] linestr=new String[100000];
int i=0;
String findstr="<cell name=\"identification-code\" column=\"E\">";
String findstr2="</cell>";
String findstr3="<cell name=\"identification-type\" column=\"D\">";
//将文件读入内存
while((linestr[i]=myBufferedReader.readLine())!=null){
i++;
}
i--;
String tmpstr=null;
for(int a=0;a<=i;a++){
//查找到证件号码
if(linestr[a].indexOf(findstr)>0){
//分离出证件号码
tmpstr=linestr[a].substring(linestr[a].indexOf(findstr)+findstr.length(),findstr(linestr[a],findstr2));
//System.out.println(tmpstr);
//如果证件号码以大写字母开始,则把上一行的证件类型改成I
if(startWithABC(tmpstr)){
if(linestr[a-1].indexOf(findstr3)>0){
linestr[a-1]="<cell name=\"identification-type\" column=\"D\">I</cell>";

}
}

}
}

//System.out.println("i="+i);
//int j=i-1;

for(int m=0;m<i;m++){
//System.out.println(linestr[m]);
myBufferedWriter.write(linestr[m]+"\n");

}

myBufferedWriter.flush();
myBufferedWriter.close();
myBufferedReader.close();
myFileR.close();
writer.close();
}


}
...全文
91 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
OKry 2009-10-16
  • 打赏
  • 举报
回复
LZ这是读XML文档吧,JAVA有专门的包可以读,我这有现成的代码
需要的话找我
干饭人之路 2009-10-16
  • 打赏
  • 举报
回复

//将文件读入内存
while((linestr[i]=myBufferedReader.readLine())!=null){

这里无论是用to_UTF8()还是changeCharset(str,"GB2312")都有乱码
干饭人之路 2009-10-16
  • 打赏
  • 举报
回复
处理xml的包我也有,但是我弄不懂为什么有乱码。

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧