Java File类boolean setReadable(boolean read_file,boolean owner_read)方法,带有示例

File Class boolean setReadable(boolean read_file,boolean owner_read) (File Class boolean setReadable(boolean read_file , boolean owner_read))

  • This method is available in package java.io.File.setReadable(boolean read_file, boolean owner_read).

    软件包java.io.File.setReadable(boolean read_file,boolean owner_read)中提供了此方法。

  • This method is used to set the read access permission to owner's or everybody's to read the file.

    此方法用于将所有者或所有人的读取访问权限设置为读取文件。

  • This method accepts two parameters the first parameter is for file access and another parameter is for file is readable by owner only or everybody's (i.e. If we pass true as a first parameter that means file is readable else false file is not readable and If we pass true as a second parameter that means file is readable by owner's only else false that's means file is readable for everybody's) .

    此方法接受两个参数,第一个参数用于文件访问,另一个参数用于文件只能由所有者或所有人读取(例如,如果将true作为第一个参数传递,则意味着文件可读,否则,则不可读错误文件,如果传递作为第二个参数,为true,表示文件所有者可以读取,否则为false,表示每个人都可以读取文件)。

  • This method may raise an exception( i.e. Security Exception) if the file written permission is not given.

    如果未提供文件写入权限,则此方法可能引发异常(即Security Exception)。

Syntax:

句法:

    boolean setReadable(Boolean read_file , boolean owner_read){
    }

Parameter(s):

参数:

We pass two objects as a parameter in the method of the File i.e read_file(Read file) and owner_read(File is readable by the owner or everybody's).

我们在File方法中将两个对象作为参数传递,即read_file(读取文件)和owner_read (所有者或所有人的文件都可以读取)。

Return value:

返回值:

The return type of this method is Boolean if it returns true so the file is readable and else returns false file is not readable.

如果此方法返回true,则该方法的返回类型为Boolean ,因此该文件可读取,否则返回false,该文件不可读。

Java程序演示setReadable()方法的示例 (Java program to demonstrate example of setReadable() 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 ReadableFileClass {
    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 file1 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt");
            File file2 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava1.txt");

            // By using setReadable(true , true)  is allowed to read 
            // the file by the owner only [i.e. First true is for 
            // whether file is readable or not (i.e. true means it is readable) 
            // and Second true is for whether file is readable by owner or 
            // everbody's (i.e. true means file is readable by owner only) ]
            if (file1.setReadable(true, true))
                System.out.println("This file " + file1.getName() + " " + "is readable  by the owner only");
            else
                System.out.println("This file " + file1.getName() + " " + "is not readable ");

            // By using setReadable(true , false)  is allowed 
            // to read the file by the everbody's 
            // [i.e. First true is for whether file is readable or not 
            // (i.e. true means it is readable) and Second true 
            // is for whether file is readable by owner or everybody's 
            // (i.e. false means file is readable by everybody's) ]
            if (file2.setReadable(true, false))
                System.out.println("This file " + file2.getName() + " " + "is readable by everybody's");
            else
                System.out.println("This file " + file2.getName() + " " + "is not readable");
        } catch (Exception e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Output

输出量

D:\Programs>javac ReadableFileClass.java

D:\Programs>java Java8 ReadableFileClass
This file myjava.txt is readable  by the owner only
This file myjava1.txt is readable by everybody's


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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值