请问如何将InputSteam 转化成OutputStream 写入文件当中?

解决方案 »

  1.   

    读取InputSteam的内容,再写入OutputStream
      

  2.   

    应该从InputStream中读取数据,然后写入OutputStream中。
      

  3.   

    private void saveDataFile(InputSteam sin, String saveToDir) throw Exception {
        OutputStream os = null ;
        String strSydate = data_System.currentTimeMillis().to_String
        File fileo = new File(saveToDir + "data_" + strSydate + ".xml" );
        try {
            os = new FileOutputStream(fileo);
            while(sin.available()>0){
                int data = sin.read();
                os.write(data);
            }
        } catch (IOException e) {
          e.printStackTrace();
    }
    请问以上这个方法可以吗?
    有个疑问 int data = sin.read() 这个从输入流里读出的为什么是个int类型?
    而且直接把 int 类型写到了 输出流 os 里边?这个可以吗?
      

  4.   

    读出来的是int类型,但实际上只读了1byte保存在了int的低8位上。
    直接把int写到输出流是可以的,因为此方法只将int的低8位写入。
      

  5.   

    先把inputStream的内容读成string或byte[]然后再把string或byte[]写入outputStream以下内容是读取的步骤呵呵,写入部分自己做吧------------------不超70码的分割线------------------private String readStringFromInputStream(InputStream inputStream,
    int contentLength, String charSetName) throws Exception {
    return new String(readByteArrayFromInputStream(inputStream,
    contentLength), charSetName);
    } private byte[] readByteArrayFromInputStream(InputStream inputStream,
    int contentLength) throws Exception {
    byte[] array = new byte[contentLength]; int readBytesNumber = 0;
    while (readBytesNumber < contentLength) {
    int readBytesNumberThisTime = inputStream.read(array,
    readBytesNumber, contentLength - readBytesNumber);
    if (readBytesNumberThisTime == -1) {
    throw new Exception("读字节错误");
    }
    readBytesNumber += readBytesNumberThisTime;
    }
    return array;
    }