Is it possible to create a File object from InputStream

前端 未结 7 831
滥情空心
滥情空心 2020-12-07 14:08

Is there any way to create a java.io.File object from an java.io.InputStream ?

My requirement is reading the File from a RAR . I am not try

7条回答
  •  既然无缘
    2020-12-07 14:48

    If you do not want to use other library, here is a simple function to convert InputStream to OutputStream.

    public static void copyStream(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
    }
    

    Now you can easily write an Inputstream into file by using FileOutputStream-

    FileOutputStream out = new FileOutputStream(outFile);
    copyStream (inputStream, out);
    out.close();
    

提交回复
热议问题