您的位置:首页 > 其它

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连接时出错");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: