有没有办法确定输入流是否是readOnly? [英] Is there a way to determine if an inputstream is readOnly?

查看:99
本文介绍了有没有办法确定输入流是否是readOnly?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想像

文件文件=新文件(inputStream)//虽然不可能。 file.canWrite

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

but i cant seem to find a solution to convert input stream into a file without writing it elsewhere. Thank you!

推荐答案

你似乎已经倒退了。

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

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);

并从<打开 InputStream code>路径,使用:

And to open an InputStream from a Path, use:

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

这篇关于有没有办法确定输入流是否是readOnly?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆