Is it possible to create a File object from InputStream

前端 未结 7 862
滥情空心
滥情空心 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 15:03

    Easy Java 9 solution with try with resources block

    public static void copyInputStreamToFile(InputStream input, File file) {  
    
        try (OutputStream output = new FileOutputStream(file)) {
            input.transferTo(output);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    
    }
    

    java.io.InputStream#transferTo is available since Java 9.

提交回复
热议问题