当前位置: 代码迷 >> 综合 >> 0008-流读取问题
  详细解决方案

0008-流读取问题

热度:92   发布时间:2023-12-21 15:31:43.0
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连接时出错");
}
}
}