日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java File类File [] listFiles()方法(带示例)

發(fā)布時(shí)間:2025/3/11 java 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java File类File [] listFiles()方法(带示例) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

File類File [] listFiles() (File Class File[] listFiles())

  • This method is available in package java.io.File.listFiles().

    軟件包java.io.File.listFiles()中提供了此方法。

  • This method is used to return the names of all the files in the form of an array of files which is represented in the filepath.

    此方法用于以文件路徑形式返回的文件數(shù)組形式返回所有文件的名稱。

  • The return type of this method is File[] i.e. It returns an array of files which is represented in filepath if the given path is directory else return null.

    此方法的返回類型為File [],即,如果給定路徑為目錄,則返回文件路徑中表示的文件數(shù)組,否則返回null。

  • This method may raise an exception( i.e. Security Exception) if the write access is not given to the file.

    如果未授予文件寫入權(quán)限,則此方法可能會(huì)引發(fā)異常(即Security Exception)。

  • This method is overridable. The first method does not accept any parameter and Second method accept one parameter(i.e FilenameFilter fnf) and Third method also accept one parameter (i.e. FileFilter).

    此方法是可重寫的。 第一種方法不接受任何參數(shù),第二種方法接受一個(gè)參數(shù)(即FilenameFilter fnf),第三個(gè)方法也接受一個(gè)參數(shù)(即FileFilter)。

Syntax:

句法:

File[] listFiles(){}

Parameter(s):

參數(shù):

We don't pass any object as a parameter in the method of the File.

我們不會(huì)在File方法中將任何對象作為參數(shù)傳遞。

Return value:

返回值:

The return type of this method is File[] i.e. it returns all files names in an array of files which is represented in a filepath.

此方法的返回類型為File [],即它以文件路徑表示的文件數(shù)組形式返回所有文件名。

Java程序演示listFiles()方法的示例 (Java program to demonstrate example of listFiles() method)

// import the File class because we will use File class methods import java.io.*;// import the Exception class because it may raise an // exception when working with files import java.lang.Exception;public class ToListSpecificFiles {public static void main(String[] args) {try {// Specify the path of file and we use double slashes to // escape '\' character sequence for windows otherwise // it will be considerable as url.File file = new File("C:\\Users\\computer clinic\\OneDrive\\Articles");// Create a FilenameFilter objectFilenameFilter fnf = new FilenameFilter() {public boolean accept(File file, String name) {return name.startsWith("J");}};// By using list(fnf) returns all the files and directories // which is represented in a file path if file path is a // directory and all file or directories starts with 'J'.String[] filelist = file.list(fnf);System.out.println("These are the name of files represented in a given directory :" + file.getPath());// By using loop to traverse the filenames and directories // in the given path .for (int i = 0; i < filelist.length; i++)System.out.println(filelist[i]);} catch (Exception e) {System.out.println("An error occurred.");e.printStackTrace();}} }

Output

輸出量

D:\Programs>javac ToListFiles.javaD:\Programs>java ToListFiles These are the name of files represented in a given directory :C:\Users\computer clinic\OneDrive\Articles C:\Users\computer clinic\OneDrive\Articles\articles_list_june.docx C:\Users\computer clinic\OneDrive\Articles\article_invoice_june.xlsx C:\Users\computer clinic\OneDrive\Articles\DurgaSoft-SCJP-notes-Part-1_JavabynataraJ.pdf C:\Users\computer clinic\OneDrive\Articles\DurgaSoft-SCJP-notes-Part-2_JavabynataraJ.pdf C:\Users\computer clinic\OneDrive\Articles\How to add characters to a string in Java.docx

翻譯自: https://www.includehelp.com/java/file-class-file-listfiles-method-with-example.aspx

總結(jié)

以上是生活随笔為你收集整理的Java File类File [] listFiles()方法(带示例)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。