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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/545381.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

案例:Redis 问题汇总和相关解决方案

本文收集了一些 Redis 使用中经常遇到的一些问题,和与之相对应的解决方案,这些内容不但会出现在实际工作中,也是面试的高频问题,接下来一起来看。 缓存雪崩 缓存雪崩是指在短时间内,有大量缓存同时过期,导致大量的请求直接查询数据库,从而对数据库造成了巨大的压力,严…

实战:Redis 集群模式(下)

上篇文章我们讲了 Redis 集群的搭建与节点的动态添加和删除,我们这里再来简单的复习一下,其中 30001~30006 是我们最初搭建的集群,而 30007 和 30008 是后面动态添加的主从节点,我们使用 --cluster info 命令来看一下主节点和槽位的分配情况,执行代码如下: $ redis-cli …

计网百度脑图

第一章概述 第二章物理层 第三章数据链路层 第四章网络层 第五章运输层

Java Integer类highestOneBit()方法与示例

整数类highestOneBit()方法 (Integer class highestOneBit() method) highestOneBit() method is available in java.lang package. maximumOneBit()方法在java.lang包中可用。 highestOneBit() method is used to find almost only single 1s bit from the leftmost side one …

转载:JAVA获取时间戳,哪个更快

转载:http://hi.baidu.com/suofang/item/96629a060a44c119eafe38cc 目前获取毫秒值大概有下面三种方法 Java代码 //方法 一 System.currentTimeMillis(); //方法 二 Calendar.getInstance().getTimeInMillis(); //方法 三 new Date().getTime(); //方法…

加餐:Redis 的可视化管理工具

因为 Redis 官方只提供了命令行版的 Redis 客户端 redis-cli,以至于我们在使用的时候会比较麻烦,通常要输入一堆命令,而且命令行版的客户端看起来也不够直观,基于以上两个原因我们需要找一个可视化的 Redis 客户端,下面是我这些年使用过的一些 Redis 可视化客户端,分享给…

第一次创建springboot框架项目

第一次创建springboot框架项目1.1_创建步骤2.1_启动时遇到的问题2.2_启动响应网页测试2.3_连接数据库尝试1.1_创建步骤 (1)创建spring项目 (2) (3) 加入引擎 下一步即可 2.1_启动时遇到的问题 &a…

JAVA学习--集合的遍历

1 Test2 public void testFor3(){3 String[] str new String[]{"AA","BB","DD"};4 for(String s : str){5 s "MM";//此处的s是新定义的局部变量,其值的修改不会对str本身造成影响。6 …

技能学习指南

经过前面文章的学习,我相信一定有一半的人看懂了,而另一半人一定是似懂非懂或者是完全不懂,如果你属于前者,那恭喜你,但如果没看懂,也没关系,本文来给你具体的解决方案。 我们来仔细回忆两件事,第一件是大学考级学的那些英语,我每个单词每个语句当时都背的滚瓜烂熟,…

转载:VB监视进程

从百度知道看的,VB监视进程: 让VB程序监视进程中的名称“Windows 任务管理器”,和“AAA”两个进程的进程名,而且进行操作:如果“Windows 任务管理器”被关闭时则自动关闭“进程某某”程序!可以用一个 Timer 定时执行下…

Java字符类isLowerCase()方法与示例

字符类isLowerCase()方法 (Character class isLowerCase() method) isLowerCase() method is available in java.lang package. isLowerCase()方法在java.lang包中可用。 isLowerCase() method is used to check whether the given char value is lowercase or not. isLowerCas…

JPA使用

JPA使用1.1_persistence使用问题1.1_persistence使用问题 import javax.persistence.*; 需要在pom文件加入依赖&#xff1a; <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId>&…

附录:更多字典操作命令

插入一个或多个元素 语法:hmset key field value [field value …] 示例: 127.0.0.1:6379> hmset myhash k1 val1 k2 val2 OK 127.0.0.1:6379> hmget myhash k1 k2 1) "val1" 2) "val2"查询一个或多个元素 语法:hmget key field [field …] 示…

Movie

悬疑片&#xff1a; 嫌疑人X的献身转载于:https://www.cnblogs.com/lnlvinso/p/4264804.html

hibernate中对象的3种状态----瞬时态、持久态、脱管态

转载&#xff1a; http://www.blogjava.net/amigoxie/archive/2007/02/11/99342.htmlHibernate的对象有3种状态&#xff0c;分别为&#xff1a;瞬时态(Transient)、 持久态(Persistent)、脱管态(Detached)。处于持久态的对象也称为PO(Persistence Object)&#xff0c;瞬时对象和…

java 根据类名示例化类_Java LocalDateTime类| ofInstant()方法与示例

java 根据类名示例化类LocalDateTime类的Instant()方法 (LocalDateTime Class ofInstant() method) ofInstant() method is available in java.time package. ofInstant()方法在java.time包中可用。 ofInstant() method is used to create an instance of LocalDateTime from t…

附录:更多字符串操作命令

键值对过期操作 a.添加键值对并设置过期时间 语法:set key value [expiration EX seconds|PX milliseconds] [NX|XX] 示例: 127.0.0.1:6379> set k1 val1 ex 1000 OK设置键值对 k1=val1,过期时间为 1000 秒。 查询键的过期时间可以使用 ttl key,如下代码所示: 127.…

ACM - 第6章 数据结构基础(2)

6.4.2 走迷宫 利用bfs寻找最短路。通过对u/m得到x&#xff0c;u%m得到y。u x * m y 将方向保存在dx[], dy[]中。 cint q[maxn*maxn]; void bfs(int x, int y) {int front 0, rear 0, d, u;u x*m y;vis[x][y] 1; fa[x][y] u; dist[x][y] 0;q[rear] u;while(front <…

springboot工程的热部署

springboot工程的热部署&#xff08;1&#xff09;第一步配置pom.xml&#xff08;2&#xff09;第二步更改IDEA设置什么是热部署配置呢&#xff1f; 我们在开发中反复修改类、页面等资源&#xff0c;每次修改后都是需要重新启动才生效&#xff0c;这样每次启动都很麻烦&#xf…

java 简单的加法 递归 从A加到B

public class Main {// 设置保存和的变量static int sum 0;public static void main(String[] args) {int begin 1, end 10;// 调用方法一 sum 0;sum add(begin, end);System.out.println(">>> 从 " begin " 开始加到 " end " 的结…