本文实例讲述了java简单列出文件夹下所有文件的方法。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
import Java.io.*; public class ListFiles { private static String s = "" ; private static BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); public static void main(String[] args) { try { s = in.readLine(); getFileList(s); in.close(); } catch (IOException e) { e.printStackTrace(); } } public static void getFileList(String directory) { File f = new File(directory); File[] files = f.listFiles(); for ( int i = 0 ; i < files.length; i++) { if (files[i].isFile()) { System.out.println( "文件:" + files[i]); } else { System.out.println( "目录:" + files[i]); // System.out.println("目录绝对地址:" + files[i].getAbsolutePath()); getFileList(files[i].getAbsolutePath()); } } } } |
希望本文所述对大家java程序设计有所帮助。