文件类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

Java文件类boolean canWrite()方法(带示例)相关推荐

  1. java 获取文件名长度_利用Java文件类File的方法,获取磁盘文件的文件名、长度、大小等特性...

    利用Java文件类File的方法,获取磁盘文件的文件名.长度.大小等特性. 如题,大神们谁可以编写一个这样的java程序?     happy530755 | 浏览 1482 次 |举报 我 ...

  2. Java文件类boolean setExecutable(boolean exec_file,boolean owner_access)方法,带示例

    文件类boolean setExecutable(boolean exec_file,boolean owner_access) (File Class boolean setExecutable(b ...

  3. Java文件类boolean canExecute()方法(带示例)

    文件类boolean canExecute() (File Class boolean canExecute()) This method is available in package java.i ...

  4. Java文件类boolean isHidden()方法(带示例)

    文件类boolean isHidden() (File Class boolean isHidden()) This method is available in package java.io.Fi ...

  5. Java文件类boolean isDirectory()方法(带示例)

    文件类boolean isDirectory() (File Class boolean isDirectory()) This method is available in package java ...

  6. Java文件类boolean setLastModified(long set_new_time)方法,包含示例

    文件类boolean setLastModified(long set_new_time) (File Class boolean setLastModified(long set_new_time) ...

  7. Java File类boolean createNewFile()方法(带示例)

    文件类布尔型createNewFile() (File Class boolean createNewFile()) This method is available in package java. ...

  8. java.util.zip 用法,Java压缩文件工具类ZipUtil使用方法代码示例

    本文实例通过Java的Zip输入输出流实现压缩和解压文件,前一部分代码实现获取文件路径,压缩文件名的更改等,具体如下: package com.utility.zip; import java.io. ...

  9. Java文件类– java.io.File

    Java File class is at the center of Java IO operations. Java File类是Java IO操作的中心. Java文件类 (Java File ...

最新文章

  1. 趣学python3(4)-数字,字符串,列表(1)
  2. 极客时间《玩转Git三剑客》之GItHub剑客
  3. c语言程序加仿真,求助。C语言的程序和仿真
  4. 大数据、云计算、AI大牛都在这里了,就等你来捞干货
  5. xpath helper小工具的安装
  6. 论文阅读02:基于深度学习的图像细粒度分类算法
  7. python-day34--进程补充
  8. 为什么判断list的时候,既要判断不等于null有要判断size大于0
  9. React Advanced 备忘
  10. sql 替换字段中的部分字符,替换指定字符
  11. 服务器内存条故障显示器,内存故障与分析
  12. 53.创建线程_beginthread
  13. 使用RssHub为网页生成RSS订阅源
  14. python爬取音乐网站排行榜_使用Python抓取Web端QQ音乐排行榜 批量下载QQ音乐到本地...
  15. c语言九宫格的递归算法,九宫格 数独 求解 算法 栈实现
  16. 冲刺大厂每日算法面试题,动态规划21天——第七天
  17. android编程异常解决 FATAL EXCEPTION: main android.view.InflateException: Binary XML file line #195: Erro
  18. S7-1200PLC求数组里数据最大值最小值FB块
  19. 如何看待人生与技术的价值
  20. 第三章 半导体中载流子的统计分布

热门文章

  1. Linux基础(使用ssh服务管理远程主机1)
  2. 计算图片相似度的方法
  3. sambd ERROR: job for smbd.service failed
  4. 代码实现——MapReduce统计单词出现次数
  5. ASP.NET Core Docker Jenkins 零基础持续集成实战
  6. Windows 7 SID 修改
  7. 复杂度O(n)倒转链表
  8. 加解密技术(Cryptography)基本概念
  9. 桌面虚拟化之用户行为审计
  10. Revit二次开发 - C#程序员的佳好选择