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

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

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

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

  • This method is used to write the file and the file is represented by the abstract filepath or in other words this method is used to test whether the application can write the file or not.

    此方法用于写入文件,并且文件由抽象文件路径表示,或者换句话说,该方法用于测试应用程序是否可以写入文件。

  • The return type of this method is Boolean i.e. it returns true or false if true that means file can be written by the application which is represented by the filepath or in other words file already exists to write and returns false that means file does not exist that means the application is not allowed to write the file.

    此方法的返回类型为Boolean,即返回true或false,如果为true则表示文件可以由文件路径表示的应用程序写入,或者换句话说,文件已存在要写入,并返回false表示文件不存在表示不允许应用程序写入文件。

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

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

Syntax:

句法:

    boolean canWrite(){
    }

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 i.e. it returns true if the file already exists and allowed to write the file which is denoted by the abstract file path returns false otherwise.

此方法的返回类型为Boolean,即如果文件已存在并且允许写入该文件(由抽象文件路径表示),则返回true,否则返回false。

Java程序演示canWrite()方法的示例 (Java program to demonstrate example of canWrite() 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 WriteFile {
    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\\java.txt");

            // By using canWrite() is allowed to write the file 
            // if file is already exists and it returns true 
            // if file is writable else false returns.
            if (file1.canWrite())
                System.out.println("This file " + file1.getName() + " " + "is writable");
            else
                System.out.println("This file " + file1.getName() + " " + "is not writable");

            // By using canWrite() is not allowed to write the file 
            // because this file is not already exists and it returns false.
            if (file2.canWrite())
                System.out.println("This file " + file2.getName() + " " + "is writable");
            else
                System.out.println("This file " + file2.getName() + " " + "is not writable");
        } catch (Exception e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Output

输出量

D:\Programs>javac WriteFile.java
D:\Programs>java WriteFile

This file C:\Users\computer clinic\OneDrive\Articles\myjava.txt is not writable
This file C:\Users\computer clinic\OneDrive\Articles\java.txt is not writable


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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值