有没有办法确定输入流是否是readOnly?

时间:2016-08-02 01:54:57

标签: java inputstream

我在想像

这样的东西

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

但我似乎找不到将输入流转换为文件而不将其写入别处的解决方案。谢谢!

1 个答案:

答案 0 :(得分:5)

你好像倒退了。

根据定义,InputStream只允许您读取字节流,无论字节流来自何处。它可能来自文件,网络套接字或自定义提供程序。根据其定义,您无论如何都只能从InputStream读取。

由于您似乎正在使用此处的文件,因此您可以使用以下方法检查文件是否为只运行当前进程的用户的

final Path path = Paths.get("path/to/my/file");

// is it writable? If no, consider "read only"
final boolean canWrite = Files.isWritable(path);

要从InputStream打开Path,请使用:

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