Java文件类boolean setReadOnly()方法(带示例)

文件类boolean setReadOnly() (File Class boolean setReadOnly())

  • This method is available in package java.io.File.setReadOnly().

    软件包java.io.File.setReadOnOn()中提供了此方法。

  • This method is used to check whether file or directory is read-only or not and this method is accessible with the File object.

    此方法用于检查文件或目录是否为只读,并且可以通过File对象访问此方法。

  • The return type of this method is Boolean i.e. it returns true or false if true that means file or directory can be open for the reading-only purpose we will not be able to modify the file or directory and returns false that means file or directory is not only read mode.

    此方法的返回类型为Boolean,即返回true或false,如果为true则意味着文件或目录可以出于只读目的而打开,我们将无法修改文件或目录,而返回false则意味着文件或目录为只读不仅是阅读模式。

  • This method will throw a security exception if write access is not given to file or directory.

    如果未授予文件或目录写访问权限,则此方法将引发安全异常。

Syntax:

句法:

    boolean setReadOnly(){
    }

Parameter(s):

参数:

We don't pass any object as a parameter in the method of the File.

我们不会在File方法中将任何对象作为参数传递。

Return value:

返回值:

The return type of this method is Boolean, it returns true or false if true that means file or directory is open for reading only purpose else returns false that means file or directory is not only in readable mode.

此方法的返回类型为Boolean ,如果为true表示文件或目录处于只读状态,则返回true或false;否则返回false表示文件或目录不仅处于可读模式,否则返回true或false 。

Java程序演示setReadOnly()方法的示例 (Java program to demonstrate example of setReadOnly() method)

// import the File class because we will use File class methods
import java.io.File;

// import the Exception class because it may raise 
// an exception when working with files
import java.lang.Exception;

public class FileReadmodeOnly {
    public static void main(String[] args) {
        try {
            // Specify the path of file and we use double slashes to 
            // escape '\' character sequence for windows otherwise 
            // it will be considerable as url.
            File file = new File("E:\\Programs\\Thread1.java");

            // By using setReadOnly() method sets the permission for 
            // file or directory is read only it returns Boolean value 
            // if true means file or directory is readable else 
            // return false that means file or directory is not open 
            // for read only purpose.
            if (file.setReadOnly())
                System.out.println("File or Directory can be open in read mode only");
            else
                System.out.println("File or Directory cannot be open in read mode only");
        } catch (Exception e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Output

输出量

D:\Programs>javac FileReadmodeOnly.java

D:\Programs>java FileReadmodeOnly
File or Directory can be open in read mode only


翻译自: https://www.includehelp.com/java/file-class-boolean-setreadonly-method-with-example.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值