博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java File类boolean isFile()方法(带示例)
阅读量:2526 次
发布时间:2019-05-11

本文共 3080 字,大约阅读时间需要 10 分钟。

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 methodsimport java.io.File;// import the Exception class because it may raise an // exception when working with filesimport 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.javaD:\Programs>java ToCheckFileThis filepath  C:\Users\computer clinic\OneDrive\Articles\myjava.txt is a fileThis filepath  C:\Users\computer clinic\OneDrive\JavaArticles is not a file

翻译自:

转载地址:http://ubtzd.baihongyu.com/

你可能感兴趣的文章
算法练习题
查看>>
学习使用Django一 安装虚拟环境
查看>>
Hibernate视频学习笔记(8)Lazy策略
查看>>
CSS3 结构性伪类选择器(1)
查看>>
IOS 杂笔-14(被人遗忘的owner)
查看>>
自动测试用工具
查看>>
前端基础之BOM和DOM
查看>>
[T-ARA/筷子兄弟][Little Apple]
查看>>
编译Libgdiplus遇到的问题
查看>>
【NOIP 模拟赛】Evensgn 剪树枝 树形dp
查看>>
java学习笔记④MySql数据库--01/02 database table 数据的增删改
查看>>
两台电脑如何实现共享文件
查看>>
组合模式Composite
查看>>
程序员最想得到的十大证件,你最想得到哪个?
查看>>
我的第一篇CBBLOGS博客
查看>>
【MyBean调试笔记】接口的使用和清理
查看>>
07 js自定义函数
查看>>
jQueru中数据交换格式XML和JSON对比
查看>>
form表单序列化后的数据转json对象
查看>>
[PYTHON]一个简单的单元測试框架
查看>>