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

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

文件类boolean isDirectory() (File Class boolean isDirectory())

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

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

  • This method is used to check whether the file is specified by filepath is a directory 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 directory else return false so it is not a directory.

    此方法的返回类型为布尔值,即,如果该方法返回true,则该值为true或false,这意味着该文件由filepath表示为目录,否则返回false,因此它不是目录。

  • This method may raise an exception(i.e. Security Exception) if the read access is not given to the file.

    如果未授予文件读取权限,则此方法可能会引发异常(即Security Exception)。

Syntax:

句法:

boolean isDirectory(){    }

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

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

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

Output

输出量

D:\Programs>javac ToCheckFileDirectory.javaD:\Programs>java ToCheckFileDirectoryThis filepath C:\Users\computer clinic\OneDrive\Articles is a directoryThis filepath C:\Users\computer clinic\OneDrive\JavaArticles is not a directory

翻译自:

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

你可能感兴趣的文章
算法导论笔记(四)算法分析常用符号
查看>>
ultraedit激活
查看>>
总结(6)--- python基础知识点小结(细全)
查看>>
亿级曝光品牌视频的幕后设定
查看>>
ARPA
查看>>
JSP开发模式
查看>>
我的Android进阶之旅------>Android嵌入图像InsetDrawable的使用方法
查看>>
Detours信息泄漏漏洞
查看>>
win32使用拖放文件
查看>>
Android 动态显示和隐藏软键盘
查看>>
raid5什么意思?怎样做raid5?raid5 几块硬盘?
查看>>
【转】how can i build fast
查看>>
null?对象?异常?到底应该如何返回错误信息
查看>>
django登录验证码操作
查看>>
(简单)华为Nova青春 WAS-AL00的USB调试模式在哪里开启的流程
查看>>
图论知识,博客
查看>>
[原创]一篇无关技术的小日记(仅作暂存)
查看>>
20145303刘俊谦 Exp7 网络欺诈技术防范
查看>>
原生和jQuery的ajax用法
查看>>
iOS开发播放文本
查看>>