您的位置 首页 java

java怎么判断是否为文件

java中可以使用java.io.File.isFile()判读是否是文件,java.io.File.isFile() 检查表示此抽象路径名的文件是否是一个正常的文件。

以下是java.io.File.isFile()方法的声明:

public boolean isFile()

参数:NA

返回值

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

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

package com.yiibai;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();      }   }}

更多java知识请关注java基础教程。

文章来源:智云一二三科技

文章标题:java怎么判断是否为文件

文章地址:https://www.zhihuclub.com/21651.shtml

关于作者: 智云科技

热门文章

网站地图