Java File类boolean isFile()方法(带示例)

File类boolean isFile() (File Class boolean isFile())

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

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

  • This method is used to check whether the file is specified by filepath is a file or not.

    此方法用于检查filepath指定的文件是否为文件。

  • The return type of this method is Boolean i.e. The value of this method is true or false if it returns true that means the file is represented by filepath is a file else return false so it is not a file.

    此方法的返回类型为布尔值,即,如果该方法返回true,则该值为true或false,这意味着该文件由filepath表示是文件,否则返回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 isFile(){
}

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 then in that case file is specified by abstract file path is a file else returns false so the file is specified is not a file.

此方法的返回类型为Boolean,即返回true,则在这种情况下,文件由抽象文件路径指定为文件,否则返回false,因此指定的文件不是文件。

Java程序演示isFile()方法的示例 (Java program to demonstrate example of isFile() 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 ToCheckFile {
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\\JavaArticles");
// By using isFile() is used to check whether the filepath 
// is a file or not. It returns true because given filepath is a file.
if (file1.isFile())
System.out.println("This filepath " + " " + file1.getAbsolutePath() + " " + "is a file");
else
System.out.println("This filepath " + " " + file1.getAbsolutePath() + " " + "is not a file");
// By using isFile() is used to check whether the filepath 
// is a file or not. It returns false because given filepath 
// is not a file .
if (file2.isFile())
System.out.println("This filepath " + " " + file2.getAbsolutePath() + " " + "is a file");
else
System.out.println("This filepath " + " " + file2.getAbsolutePath() + " " + "is not a file");
} catch (Exception e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}

Output

输出量

D:\Programs>javac ToCheckFile.java
D:\Programs>java ToCheckFile
This filepath  C:\Users\computer clinic\OneDrive\Articles\myjava.txt is a file
This filepath  C:\Users\computer clinic\OneDrive\JavaArticles is not a file

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

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

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

相关文章

要加油!

现实中我容易佩服一个人。 一个顽强的女人,一个艰苦奋斗的男人..... 但是在网络的世界里,我没有佩服过几个,但是不得不说的就是冰河。同样的年龄人家做的事情和我们做的事情差距是多么的大,真的想想心里都是天壤之别。 比一比才知…

Java DataOutputStream writeInt()方法及示例

DataOutputStream类writeInt()方法 (DataOutputStream Class writeInt() method) writeInt() method is available in java.io package. writeInt()方法在java.io包中可用。 writeInt() method is used to write the given integer value to the basic DataOutputStream as 4 b…

python安卓自动化实现方法_uiautomator +python 实现安卓UI自动化

简单实例注:安卓6.0以上的手机不会自动安装app-uiautomator.apk和app-uiautomator-test.apk,需要手动安装,否则报错ioerror RPC server not starteduiautomator pythonHTMLTestRunner 安卓UI自动化实现#coding:utf-8from uiautomator importD…

ES6特性之:Spread操作符

Spread操作符(...),也称作展开操作符,作用是将可迭代的(Iterable)对象进行展开。 比如有2个数组,我们要将其中一个数组中所有元素插入到另一个数组中,通过Spread操作符,就可以这样进行: var fruits ["…

Java类class isMemberClass()方法及示例

类的类isMemberClass()方法 (Class class isMemberClass() method) isMemberClass() method is available in java.lang package. isMemberClass()方法在java.lang包中可用。 isMemberClass() method is used to check whether the underlying class is a member class or not.…

velocity自定义函数_velocity基本语法和总结

一:基本语法:1、#set(#a "a")$a ##输出语句时直接写变量的名称即可2、判断语句:#if($a "a") ##判断语句没有括号,也是直接输出$a3、数组:#set($arry [0..10])$foreach($i in $arry)$i ##换行#e…

docker-machine指定cpu个数

序 给本机的一个服务压测,结果半天qps上不了万,而且经常跑满cpu,搞半天发现,docker里头才1核1G内存。原来boot2docker默认给docker-machine分配1个cpu和1G内存。 修改配置 docker-machine create \--driver virtualbox \--virtual…

Java ClassLoader findResources()方法与示例

ClassLoader类findResources()方法 (ClassLoader Class findResources() method) findResources() method is available in java.lang package. findResources()方法在java.lang包中可用。 findResources() method is used to find all the resources with the given resource …

Java ByteArrayInputStream mark()方法与示例

ByteArrayInputStream类mark()方法 (ByteArrayInputStream Class mark() method) mark() method is available in java.util package. mark()方法在java.util包中可用。 mark() method is used to set the current mark position in the stream from where read or write can b…

java mediainfo.dll_MediaInfo库的简单使用

想到一个问题, 如何获得一个图像文件(比如jpg, bmp, png)的信息. 自己查查文件的格式, 写一个解析, 应该不困难; 但是找了下现成的, 发现MediaInfo库已经可以非常好的实现需要的功能了.MediaInfo可以在sourceforge上找到, 是一个解析视频,音频, 图片等媒体文件的库. 可以得到文…

Redis配置和常用命令

1 redis.conf配置文件:2 引用3 #是否作为守护进程运行4 daemonize yes5 #配置pid的存放路径及文件名,默认为当前路径下6 pidfile redis.pid7 #Redis默认监听端口8 port 63799 #客户端闲置多少秒后,断开连接 10 timeout 300 11 #日志显示级别 …

oracle中dbms_DBMS中的功能依赖性和属性关闭

oracle中dbms功能依赖 (Functional Dependency) A relational Database management System (RDBMS) represents the database o a collection of relations/tables. A functional dependency is a constraint between two sets of attributes in a relation. It is the propert…

java invoke 泛型_利用Java反射机制和泛型,全自动解析json

有啦这个简直,太爽啦,利用Java 反射机制,利用Class 就可以得到 类的 变量 Field[] fieldscls.getDeclaredFields();还可以通过类中 的方法名字 去执行这个方法m1 cls.getDeclaredMethod(getMothodName(fields[j].getName()), String.class)…

2_C语言中的数据类型 (四)整数与无符号数

1.1 sizeof关键字 sizeof是c语言关键字,功能是求指定数据类型在内存中的大小,单位:字节 sizeof与size_t类型 1.1 int类型 1.1.1 int常量,变量 int就是32位的一个二进制整数,在内存当中占据4个字节…

python 示例_Python TextCalendar类别| pryear()方法与示例

python 示例Python TextCalendar.pryear()方法 (Python TextCalendar.pryear() Method) pryear() method is an inbuilt method of the TextCalendar class of calendar module in Python. It works on text calendars. It uses an instance of TextCalendar class and prints …

Spring实战——通过Java代码装配bean

上篇说的是无需半行xml配置完成bean的自动化注入。这篇仍然不要任何xml配置,通过Java代码也能达到同样的效果。 这么说,是要把上篇的料拿出来再煮一遍? 当然不是,上篇我们几乎都在用注解的方式如ComponentScan Component等就完成了…

java 谓词_java8-谓词(predicate)

传递代码我们首先看一个例子,假设你有一个 Apple 类,它有一个getColor方法,还有一个变量inventory保存着一个Apples的列表。你可能想要选出所有的绿苹果,并返回一个列表。通常我们用筛选(filter)一词来表达这个概念。在 Java 8之前…

getlong_Java LocalDateTime类| 带示例的getLong()方法

getlongLocalDateTime类的getLong()方法 (LocalDateTime Class getLong() method) getLong() method is available in java.time package. getLong()方法在java.time包中可用。 getLong() method is used to get the value as long for the given temporal field from this dat…

java.io和util的区别_Java NIO与IO的区别和比较

Java NIO与IO的区别和比较导读J2SE1.4以上版本中发布了全新的I/O类库。本文将通过一些实例来简单介绍NIO库提供的一些新特性:非阻塞I/O,字符转换,缓冲以及通道。一. 介绍NIONIO包(java.nio.*)引入了四个关键的抽象数据类型,它们共…