下面是需要用流去读的xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<page pageId ="deskTop" pageTitle="我的桌面" pageVersion="1.0" cache="Check">
<body bgImage="$bgImage$">
<gridTable nameId="gid1" location="" size="" rows="3" cols="3">
<gridItem text="移动产品" url="$server_path$/Api.ashx?function=getGrpProduct&id_u=$id_u$" img="local://resources/desktop/2.png"/>
<gridItem text="集团产品" url="$server_path$/Api.ashx?function=getProduct&id_u=$id_u$" img="local://resources/desktop/3.png"/>
<gridItem text="知识库" url="$server_path$/Api.ashx?function=getKnowledgeTypes&id_u=$id_u$" img="local://resources/desktop/0.png"/>
<gridItem text="信息采集" url="$server_path$/Api.ashx?function=getCollType&id_u=$id_u$" img="local://resources/desktop/1.png"/>
<gridItem text="在线交流" url="$server_path$/Api.ashx?function=getMyQuestionList&id_u=$id_u$&id_acc=$id_acc$" img="local://resources/desktop/4.png"/>
<gridItem text="通讯录" url="$server_path$/Api.ashx?function=getAddrTypes&id_u=$id_u$" img="local://resources/desktop/5.png"/>
<gridItem text="广播信息" url="$server_path$/Api.ashx?function=getNewsList&id_u=$id_u$" img="local://resources/desktop/6.png"/>
<gridItem text="辅助软件" url="$server_path$/Api.ashx?function=getHelpTool" img="local://resources/desktop/7.png"/>
<gridItem text="退出系统" url="cmd://exit_app" img="local://resources/desktop/8.png"/>
</gridTable>
</body>
</page>
首先我是用fileInputStream去读的,代码如下:
public static String readXml(String path){
StringBuffer sb = new StringBuffer();
try {
InputStream is = new FileInputStream(path);
byte[] buffer = new byte[1024];
while(true){
int length = is.read(buffer,0,1024);
if(length == -1) break;
length = is.read(buffer,0,1024);
sb.append(new String(buffer));
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString().trim();
}
可是读出来的后台得到的结果是:
i.ashx?function=getAddrTypes&id_u=39" img="local://resources/desktop/5.png"/>
<gridItem text="骞挎挱淇℃伅" url="http://192.168.89.131/Api.ashx?function=getNewsList&id_u=39" img="local://resources/desktop/6.png"/>
<gridItem text="杈呭姪杞欢" url="http://192.168.89.131/Api.ashx?function=getHelpTool" img="local://resources/desktop/7.png"/>
<gridItem text="閫?鍑虹郴缁?" url="cmd://exit_app" img="local://resources/desktop/8.png"/>
</gridTable>
</body>
</page>es/desktop/3.png"/>
<gridItem text="鐭ヨ瘑搴?" url="http://192.168.89.131/Api.ashx?function=getKnowledgeTypes&id_u=39" img="local://resources/desktop/0.png"/>
<gridItem text="淇℃伅閲囬泦" url="http://192.168.89.131/Api.ashx?function=getCollType&id_u=39" img="local://resources/desktop/1.png"/>
<gridItem text="鍦ㄧ嚎浜ゆ祦" url="http://192.168.89.131/Api.ashx?function=getMyQuestionList&id_u=39&id_acc=64" img="local://resources/desktop/4.png"/>
<gridItem text="閫氳褰?" url="http://192.168.89.131/Ap
不但错行,而且还存在乱码,所以我换成用reader.read()方法来读取,
Reader r = new InputStreamReader(is,"utf-8");
这样一来乱码问题是解决了,不过还是有错行的问题,所以正确代码如下:
public static String readXml(String path){
StringBuffer sb = new StringBuffer();
try {
InputStream is = new FileInputStream(path);
Reader r = new InputStreamReader(is,"utf-8");
BufferedReader br = new BufferedReader(r);
String str = "";
while(true){
//读取一行文字数据
str = br.readLine();
if(str == null){
break;
}
sb.append(str+"\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString().trim();
}
用BufferedReader.readLine()方法就可以了!
分享到:
相关推荐
java.io.CharConversionException: isHexDigit.最近项目运行出现了这个问题,刚开始就知道是编码问题,怎么改编码都不正确,在网上搜到的文章。
java.io.Reader 和 java.io.InputStream 的区别
ajax,js传值到后台,后台出现java.io.CharConversionException: isHexDigit 异常
使用System.IO.Ports读取COM口数据 关于C#串口编程的介绍
java.io.Serializable序列化问题
网上找来了相关java文件,改文件包主要用于解决类似找不到sun.io.CharToByteConverter类及相关方法情况,放到对应引用文件目录下编译即可。 PS:java文件编码格式为UTF-8,如果有其它编码格式,自己调整一下就行,我...
gnu.io.CommDriver.class gnu.io.CommPort.class gnu.io.CommPortEnumerator.class gnu.io.CommPortIdentifier.class gnu.io.CommPortOwnershipListener.class gnu.io.Configure.class gnu.io.I2C.class gnu.io.I2...
NULL 博文链接:https://yizhilong28.iteye.com/blog/1154098
串口通讯需要的jar包,gnu.io.SerialPort; gnu.io.CommPortIdentifier; gnu.io.SerialPort; gnu.io.SerialPortEvent; gnu.io.SerialPortEventListener; gnu.io.UnsupportedCommOperationException; Copy RXTXcomm...
java.io clojure.java.io 的 JK7 java.nio.file.Path 兼容性依赖信息该库托管在 Releases 上。 依赖: [me.moocar/java.io " 0.1.0 " ]用法是 JDK7 中引入的文件路径的抽象。 这个库提供了和 Paths 之间的兼容性。 ...
java.io包详细解说,讲得很详细 同类文档:Java-从Decorator,Adapter模式看Java IO库,已上传
Eclipse 进行 ANT 时错误 Javadoc failed java.io.IOException Cannot run program javadoc
例如,从键盘读取数据、在网络上交换数据、打印报表、读写文件信息等,都要涉及数据输入输出的处理。在面向对象语言中,输入和输出都是通过数据流来实现的。在Java中,处理数据流的类主要被放在java.io包中 。
http://blog.csdn.net/maoxiang/archive/2008/09/20/2954553.aspx 详细见
socket.io,socket.io-client 用于java或android连接nodejs websocket
Layaair Socket.io文件 Layaair Socket.io文件 文档https://blog.csdn.net/nicepainkiller/article/details/91043252 https://blog.csdn.net/nicepainkiller/article/details/91043252
因为项目中需要实现后端推荐消息到前端,经过研究和讨论,最终决定用Socket.io实现消息的推送,而在SpringBoot用Socket.io实现消息推送在网上的例子太少了,本人研究了一天的Socket.io,终于对Socket.io的应用比较...
Java.nio 与Java.io比较
用java对文件操作的一些代码(包括文件读取,移动,删除,复制......)
java io读取文件的技术,最近用到了大数据量文件的读取,研究下