InputStream is = new ByteArrayInputStream(pdfByteArray); byte[] bytearray = new byte[1024]; try {     OutputStream os = new FileOutputStream(PDFDir + PDFFileName);     do{         is.read(bytearray, 0, 1024);         os.write(bytearray);     }while (is.available() > 0);     is.close();     os.close(); } catch (FileNotFoundException e1) {         e1.printStackTrace(); } catch (IOException e) {     e.printStackTrace(); } 

解决方案 »

  1.   

    int len = is.read(bytearray);
    os.write(bytearray,0,len);
      

  2.   


    没有这个方法吧,int len = is.read(bytearray);
      

  3.   

    public int read (byte[] buffer)
    Equivalent to read(buffer, 0, buffer.length).public int read (byte[] buffer, int byteOffset, int byteCount)
    Reads up to byteCount bytes from this stream and stores them in the byte array buffer starting at byteOffset. Returns the number of bytes actually read or -1 if the end of the stream has been reached.你应该是读多少,写多少。
    你的代码里,把数据读到一个长度为1024的byte数组里。但这个数组不一定能够被填满。而你写的总是1024长度。所以你拷贝完的文件总会比原始文件稍微大一点点。
      

  4.   

    赐教不敢说。你每次读取的buffer 的长度为1024,这个值比较小,只要没有其他的特殊操作,导致文件读取变慢的情况下,每次读取1024个字节的buffer 是没问题的。你如果设置成一个比较大的值,比如 512 × 1024 ,或者更大,你基本上没有成功的机会了。因为你的文件里加入了很多非法的数据,而且文件的长度要变得大了很多。