歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java列出文件夾中的所有文件

Java列出文件夾中的所有文件

日期:2017/3/1 10:35:52   编辑:Linux編程

It shows that we can list all the files in a specified folder, if it exists.

[java]

  1. package com.han;
  2. import java.io.File;
  3. /**
  4. * It shows that we can list all the files in a specified folder, if it exists.
  5. * @author han
  6. *
  7. */
  8. public class ListFiles {
  9. public static void main(String[] args) {
  10. // TODO Auto-generated method stub
  11. File file=new File("/home/han/Documents/word");
  12. // System.out.println(file.getName());
  13. if (file.isDirectory()){
  14. File[] fl=file.listFiles();
  15. for (int i=0;i<fl.length;i++){
  16. System.out.println(fl[i]);
  17. }
  18. }
  19. else{
  20. System.out.println("File不是目錄。");
  21. }
  22. }
  23. }
Copyright © Linux教程網 All Rights Reserved