0008-流读取问题
2016-02-03 09:23
351 查看
BufferedReader in = null;
PrintWriter out = null;
StringBuffer resultBuffer = new StringBuffer();
try {
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream());
String xml;
// 是否添加到resultBuffer中
boolean flag = false;
while ((xml = in.readLine()) != null) {
if (!flag&&xml.indexOf("<ChanResult") != -1) {
flag = true;
}
if (flag) {
resultBuffer.append(xml);
//读完后直接结束不去判断while null 可能导致reset问题
if (xml.indexOf("</ChanResult>") != -1) {
break;
}
}
}
//读完后将开关置为false
flag = false;
} catch (IOException ex) {
LOG.error("读取信息时发生IO错误 + resultBuffer.toString() , ex);
} finally {
try {
if(in != null){
in.close();
}
} catch (Exception e) {
LOG.error("关闭与系统连接的输入流时出错");
}
try {
if(out != null){
out.close();
}
} catch (Exception e) {
LOG.error("关闭与系统连接的输出流时出错");
}
try {
if(socket!= null){
socket.close();
}
} catch (Exception e) {
LOG.error("关闭与系统连接的socket连接时出错");
}
}
}
PrintWriter out = null;
StringBuffer resultBuffer = new StringBuffer();
try {
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream());
String xml;
// 是否添加到resultBuffer中
boolean flag = false;
while ((xml = in.readLine()) != null) {
if (!flag&&xml.indexOf("<ChanResult") != -1) {
flag = true;
}
if (flag) {
resultBuffer.append(xml);
//读完后直接结束不去判断while null 可能导致reset问题
if (xml.indexOf("</ChanResult>") != -1) {
break;
}
}
}
//读完后将开关置为false
flag = false;
} catch (IOException ex) {
LOG.error("读取信息时发生IO错误 + resultBuffer.toString() , ex);
} finally {
try {
if(in != null){
in.close();
}
} catch (Exception e) {
LOG.error("关闭与系统连接的输入流时出错");
}
try {
if(out != null){
out.close();
}
} catch (Exception e) {
LOG.error("关闭与系统连接的输出流时出错");
}
try {
if(socket!= null){
socket.close();
}
} catch (Exception e) {
LOG.error("关闭与系统连接的socket连接时出错");
}
}
}
相关文章推荐
- 14.3 事务隔离级别
- gcov覆盖率测试
- 14.2 事务的ACID属性
- [Python module]使用threading模块实现多线程编程一[综述]
- Spring 构造系统UI资源
- (9)shell printf命令:格式化输出
- java中@value的环境配置
- 13.5 小结
- 13.4.2 性能比较
- WoodenSticks(区间贪心类题目)
- Android开发之百度翻译
- SAP 库存相关表格
- 前端
- cmder显示UTF-8字体
- abap面向对象的报表
- cgroup学习(八)——CPUSET子系统
- 13.3 DELETE
- cmder显示UTF-8字体
- 13.2 UPDATE
- cgroup学习(七)——cpu子系统