Java - File list() 方法



描述

Java 的File list()方法返回由該抽象路徑名定義的目錄中檔案和目錄的陣列。如果抽象路徑名不表示目錄,則該方法返回 null。

宣告

以下是java.io.File.list()方法的宣告:

public String[] list()

引數

返回值

該方法返回由該抽象路徑名錶示的目錄中的檔案和目錄陣列。

異常

SecurityException - 如果存在安全管理器並且其SecurityManager.checkRead(java.lang.String)方法拒絕讀取檔案的訪問許可權

示例 1

以下示例演示了 Java File list() 方法的用法。我們建立了一個 File 引用。然後我們使用提供的目錄中存在的 test 目錄建立一個 File 物件。然後我們使用 list() 方法將 test 目錄中存在的檔案列表獲取到一個字串陣列中。然後迭代此陣列以列印給定目錄中可用的檔案。

package com.tutorialspoint;
import java.io.File;
public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      String[] paths;            
      try {    
      
         // create new file
         f = new File("F:/test");
                                 
         // array of files and directory
         paths = f.list();
            
         // for each name in the path array
         for(String path:paths) {
         
            // prints filename and directory name
            System.out.println(path);
         }
         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

test.txt
Test1.txt

示例 2

以下示例演示了 Java File list() 方法的用法。我們建立了一個 File 引用。然後我們使用我們正在執行此程式的當前目錄“.”建立一個 File 物件。然後我們使用 list() 方法將當前目錄中存在的檔案列表獲取到一個字串陣列中。然後迭代此陣列以列印給定目錄中可用的檔案。

package com.tutorialspoint;
import java.io.File;
public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      String[] paths;            
      try {    
      
         // create new file
         f = new File(".");
                                 
         // array of files and directory
         paths = f.list();
            
         // for each name in the path array
         for(String path:paths) {
         
            // prints filename and directory name
            System.out.println(path);
         }
         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

.classpath
.project
.settings
app.log
bin
properties.txt
properties.xml
src
test.txt

示例 3

以下示例演示了 Java File list() 方法的用法。我們建立了一個 File 引用。然後我們使用提供的目錄中不存在的 test3 目錄建立一個 File 物件。然後我們使用 list() 方法將 test 目錄中存在的檔案列表獲取到一個字串陣列中。然後迭代此陣列以列印給定目錄中可用的檔案。由於資料夾不存在,陣列將為 null,我們列印一條訊息 - “檔案不存在”。

package com.tutorialspoint;
import java.io.File;
public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      String[] paths;
      try {    

         // create new file
         f = new File("F:/Test3");
		 
         // array of files and directory
         paths = f.list();

         if(paths != null) {
            // for each name in the path array
            for(String path:paths) {

               // prints filename and directory name
               System.out.println(path);
            }
         }else {
            System.out.println("Files are not present");
         }
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Files are not present
java_file_class.htm
廣告
© . All rights reserved.