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則表示文件可以由文件路徑表示的應用程序?qū)懭?#xff0c;或者換句話說,文件已存在要寫入,并返回false表示文件不存在表示不允許應用程序?qū)懭胛募?
This method may raise an exception( i.e. Security Exception) if the write access is not given to the file.
如果未授予文件寫入權限,則此方法可能會引發(fā)異常(即Security Exception)。
Syntax:
句法:
boolean canWrite(){}Parameter(s):
參數(shù):
We don't pass any object as a parameter in the method of the File.
我們不會在File方法中將任何對象作為參數(shù)傳遞。
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");elseSystem.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");elseSystem.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 WriteFileThis 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
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結
以上是生活随笔為你收集整理的Java文件类boolean canWrite()方法(带示例)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c printf 段错误_错误:预期声明
- 下一篇: Java字符串indexOf(int c