package com.wxd.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
public class te {
// 把xml文件读成 字符流
public static String xmlToLiu(String path) throws Exception {
File file = new File(path);
if (!file.exists() || file.isDirectory()) {
throw new FileNotFoundException();
}
// 以"GB2312"编码,解决中文乱码问题
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), "utf-8");
BufferedReader br = new BufferedReader(read);
String temp = null;
StringBuffer sb = new StringBuffer();
temp = br.readLine();
while (temp != null) {
sb.append(temp + "\n");
temp = br.readLine();
}
br.close();
read.close();
return sb.toString();
}
public static void main(String[] args) throws Exception {
String path = "E:\\1.xml";
System.out.println(xmlToLiu(path));
}
}
1.xml 中文件内容为:
<?xml version="1.0" encoding="GB2312" standalone="yes" ?>
<TX>
<REQUEST_SN>请求序列码</REQUEST_SN>
<CUST_ID>客户号</CUST_ID>
<USER_ID>操作员号</USER_ID>
<PASSWORD>密码</PASSWORD>
<TX_CODE>6W0100</TX_CODE>
<LANGUAGE>CN</LANGUAGE>
<TX_INFO>
<ACC_NO>账号</ACC_NO>
</TX_INFO>
</TX>
控制台输出:
注意:使用 InputStreamReader read = new InputStreamReader( new FileInputStream(file), "utf-8"); 时,后面的