English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
في هذا الدليل، سنتعلم كيفية قائمة جميع الملفات والمجلدات الموجودة في الدليل.
ملف Javaيستخدم طريقة list() للفئة لقائمة جميع الملفات والمجلدات الموجودة في الدليل. ويقوم بتقديم جميع الملفات والمجلدات كعدد من الأشكال النصية.
import java.io.File; class Main { public static void main(String[] args) { //Create a file object File file = new File("C:\\Users\\Guest User\\Desktop\\Java File\\List Method"); //Returns an array of all files String[] fileList = file.list(); for(String str : fileList) { System.out.println(str); } } }
Output Result
.vscode file.txt directory newFile.txt
In the above example, we created a file object named file. This object stores information about the specified path.
File file = new File("C:\\Users\\Guest User\\Desktop\\Java File\\List Method");
We have used the list() method to list all the files and subdirectories existing in the specified path.
file.list();
Note: We useddouble backslash. This is because theCharacterIn Java, \ is used asEscape CharacterTherefore, the first backslash is used as an escape character for the second.