Is there a way to determine if an inputstream is readOnly?

后端 未结 1 585
天涯浪人
天涯浪人 2021-01-29 11:43

I was thinking something like a

File file =new File(inputStream) // not possible though. file.canWrite,

but i cant seem to find a solution to conv

1条回答
  •  时光说笑
    2021-01-29 12:03

    You seem to have it backwards.

    By definition, an InputStream will only ever allow you to read a stream of bytes, wherever that stream of bytes comes from. It may be from a file, or a network socket, or a custom provider. By its very definition, you can only read from an InputStream anyway.

    Since you seem to be working with files here, you can check whether a file is read only for the user running the current process using:

    final Path path = Paths.get("path/to/my/file");
    
    // is it writable? If no, consider "read only"
    final boolean canWrite = Files.isWritable(path);
    

    And to open an InputStream from a Path, use:

    try (
        final InputStream in = Files.newInputStream(path);
    ) {
        // work with "in"
    }
    

    0 讨论(0)
提交回复
热议问题