美文网首页
将inputStream以文件的格式写入本地

将inputStream以文件的格式写入本地

作者: 我想起个好名字 | 来源:发表于2018-07-10 14:43 被阅读0次

private static void inputstreamtofile(InputStream ins,File file)throws Exception {

//可行,但是第二次不行

//    FileOutputStream fos = new FileOutputStream(file.getPath());

//

//    byte[] b = new byte[1024];

//

//    while((ins.read(b)) != -1){

//

//      fos.write(b);

//

//    }

//

//    ins.close();

//

//    fos.close();

//另一种

      try {

OutputStream os =new FileOutputStream(file);

        int bytesRead =0;

        byte[] buffer =new byte[8192];

        while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {

os.write(buffer, 0, bytesRead);

        }

os.close();

        ins.close();

      }catch (Exception e) {

e.printStackTrace();

      }

}

相关文章

网友评论

      本文标题:将inputStream以文件的格式写入本地

      本文链接:https://www.haomeiwen.com/subject/jqgfpftx.html