java如何判断是否为文件

互联网 19-11-14
java.io.File.isFile() 检查表示此抽象路径名的文件是否是一个正常的文件。

表示此抽象路径名的文件是一个文件,该方法返回true,否则该方法返回false。

异常 (推荐学习:java课程)

例子

下面的示例演示java.io.File.isFile()方法的用法。

package com.test;  import java.io.File;  public class FileDemo {    public static void main(String[] args) {              File f = null;       String path;       boolean bool = false;              try{          // create new file          f = new File("c:");                       // true if the file path is a file, else false          bool = f.isFile();                    // get the path          path = f.getPath();                    // prints          System.out.println(path+" is file? "+ bool);                          // create new file          f = new File("c:/test.txt");                    // true if the file path is a file, else false          bool = f.isFile();                    // get the path          path = f.getPath();                    // prints          System.out.print(path+" is file? "+bool);                 }catch(Exception e){          // if any error occurs          e.printStackTrace();       }    } }

让我们编译和运行上面的程序,这将产生以下结果:

c: is file? false c:est.txt is file? true

以上就是java如何判断是否为文件的详细内容,更多内容请关注技术你好其它相关文章!

来源链接:
免责声明:
1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险
2.本文版权归属原作所有,仅代表作者本人观点,不代表本站的观点或立场
标签: java
上一篇:php获取远程图片并下载保存到本地的方法分析 下一篇:oracle端口号是多少

相关资讯