• 技术文章 >Java >Java入门

    java如何判断是否为文件

    (*-*)浩(*-*)浩2019-11-14 13:23:43原创1134

    java.io.File.isFile() 检查表示此抽象路径名的文件是否是一个正常的文件。

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

    异常 (推荐学习:java课程

    SecurityException -- 如果安全管理器存在并且其SecurityManager.checkRead(java.lang.String) 方法拒绝对文件的读访问

    例子

    下面的示例演示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如何判断是否为文件的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:java
    上一篇:java static是什么 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • javascript与java的区别• java找不到main方法?• java中char占几个字节?• Java中常见的几种锁
    1/1

    PHP中文网