首頁  >  文章  >  Java  >  java如何判斷是否為文件

java如何判斷是否為文件

(*-*)浩
(*-*)浩原創
2019-11-14 13:23:432408瀏覽

java.io.File.isFile() 檢查表示此抽象路徑名稱的檔案是否為正常的檔案。

java如何判斷是否為文件

表示此抽象路徑名的文件是一個文件,該方法傳回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