我在做一个项目时发现:当我用常用的下载方法 下载xml到sd卡中 然后读取xml解析时 总是有如下异常:

但程序仍能运行 只是xml解析后的条数变少了 我觉得应该是解析过程中遇到了不能解析的字符

但检查服务器端的xml并未发现错误 (我还曾一度认为是网络不佳导致的,现在想象真是可笑的误区)

之后我检查了下载到本地sd卡的xml文件,用notepad打开后 错误发现了:


经过百度 谷歌 的各种搜素也未找到缘由

我开始认为是我的下载与写入sd 的方法有问题

但是从论坛或是文章中 看到很多下载文件与写入的方法都是这样的:

Java代码

  1. /**
  2.      * is流写入sd卡
  3.      * @param path
  4.      * @param fileName 路径+名字
  5.      * @param is
  6.      * @return
  7.      */
  8. public File write2SDFromIS(String path,String fileName,InputStream is){  
  9.         File file = null;  
  10.         OutputStream os = null;  
  11. try {  
  12.             createSDDir(path);  
  13.             file = createSDFile(path+fileName);  
  14.             os = new FileOutputStream(file);  
  15.  
  16. byte buffer[] = new byte[1024];  
  17. while(is.read(buffer) != -1){  
  18.                 os.write(buffer);  
  19.             }  
  20. //          int bufferLength = 0;
  21. //          while ( (bufferLength = is.read(buffer)) > 0 ) {
  22. //              os.write(buffer, 0, bufferLength);
  23. //          }
  24.             os.flush();  
  25.  
  26.  
  27.         } catch (IOException e) {  
  28. // TODO Auto-generated catch block
  29.             e.printStackTrace();  
  30.         }finally{  
  31. try {  
  32.                 os.close();  
  33.             } catch (IOException e) {  
  34. // TODO Auto-generated catch block
  35.                 e.printStackTrace();  
  36.             }  
  37.         }  
  38. return file;  
  39.  
  40.     } 

/** * is流写入sd卡 * @param path * @param fileName 路径+名字 * @param is * @return */ public File write2SDFromIS(String path,String fileName,InputStream is){ File file = null; OutputStream os = null; try { createSDDir(path); file = createSDFile(path+fileName); os = new FileOutputStream(file); byte buffer[] = new byte[1024]; while(is.read(buffer) != -1){ os.write(buffer); } // int bufferLength = 0; // while ( (bufferLength = is.read(buffer)) > 0 ) { // os.write(buffer, 0, bufferLength); // } os.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return file; }

我是个初学者 也没多想 就使用了这段代码。但经过多次测试后发现

Java代码

  1. byte buffer[] = new byte[1024];  
  2. while(is.read(buffer) != -1){  
  3.                 os.write(buffer);  
  4.             } 

byte buffer[] = new byte[1024]; while(is.read(buffer) != -1){ os.write(buffer); }

这段代码导致了下载后出现NUL符(有时xml较大时 会有部分乱码 结尾不全的情况)

我换用了如下代码(注释的内容):

Java代码

  1. byte buffer[] = new byte[1024];  
  2. int bufferLength = 0;  
  3. while ( (bufferLength = is.read(buffer)) > 0 ) {  
  4.                 os.write(buffer, 0, bufferLength);  
  5.             } 

byte buffer[] = new byte[1024]; int bufferLength = 0; while ( (bufferLength = is.read(buffer)) > 0 ) { os.write(buffer, 0, bufferLength); }

便解决了 上述问题

我是个菜鸟 如果说原因我只能猜测 可能是错误的写法每次写入固定1024的长度

而换用有bufferLength的方法write 则规定了长度 避免了不足1024的用NUL代替了吧!