java如何判断一个路径是否为文件呢?

书欣 Java经验 发布时间:2022-08-22 11:50:05 阅读数:7076 1
下文笔者讲述使用java代码判断路径是否为文件的方法分享,如下所示
实现思路:
    使用java.io.File.isFile()方法即可判断一个路径是否为文件
 java.io.File.isFile()方法会返回一个布尔值   
    当返回true时,代表为文件
	  返回false时,代表不是文件
例:

package com.java265;
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();
      }
   }}
  
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202208/16611402494264.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者