如何使用Java设置文件的只读访问权限 [英] how to set read only access for file using java

查看:309
本文介绍了如何使用Java设置文件的只读访问权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为使用Java InputStream API创建和写入的文件设置只读访问权限.

How to set read-only access for a file that's been created and written using java InputStream API.

推荐答案

在我看来,最好先检查文件是否已创建或存在,然后再设置只读标志.

In my opinion, it's good to check whether the file has been created or exists first and then set the read only flag.

    File file = new File("C:/path/file.txt");
    if (file.exists()) {
        file.setReadOnly();
    } else {
        System.out.println("File does not exists.");
    }

这篇关于如何使用Java设置文件的只读访问权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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