java.io读取文件疑惑解析
来源:优易学  2011-12-21 16:45:50   【优易学:中国教育考试门户网】   资料下载   IT书店

 

  return sb.toString().trim();

  }

  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();

  }

  可是读出来的后台得到的结果是:

  Xml代码

  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&amp;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&amp;id_u=39" img="local://resources/desktop/0.png"/>

  <gridItem text="淇℃伅閲囬泦" url="http://192.168.89.131/Api.ashx?function=getCollType&amp;id_u=39" img="local://resources/desktop/1.png"/>

  <gridItem text="鍦ㄧ嚎浜ゆ祦" url="http://192.168.89.131/Api.ashx?function=getMyQuestionList&amp;id_u=39&amp;id_acc=64" img="local://resources/desktop/4.png"/>

  <gridItem text="阃氲褰?" url="http://192.168.89.131/Ap

  i.ashx?function=getAddrTypes&amp;id_u=39" img="local://resources/desktop/5.png"/>

  <gridItem text="骞挎挱淇℃伅" url="http://192.168.89.131/Api.ashx?function=getNewsList&amp;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&amp;id_u=39" img="local://resources/desktop/0.png"/>

  <gridItem text="淇℃伅閲囬泦" url="http://192.168.89.131/Api.ashx?function=getCollType&amp;id_u=39" img="local://resources/desktop/1.png"/>

  <gridItem text="鍦ㄧ嚎浜ゆ祦" url="http://192.168.89.131/Api.ashx?function=getMyQuestionList&amp;id_u=39&amp;id_acc=64" img="local://resources/desktop/4.png"/>

  <gridItem text="阃氲褰?" url="http://192.168.89.131/Ap 不但错行,而且还存在乱码,所以我换成用reader.read()方法来读取,

  Java代码

  Reader r = new InputStreamReader(is,"utf-8");

  Reader r = new InputStreamReader(is,"utf-8");

  这样一来乱码问题是解决了,不过还是有错行的问题,所以正确代码如下:

  Java代码

  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();

  }

上一页  [1] [2] 

责任编辑:小草

文章搜索:
 相关文章
热点资讯
热门课程培训