importjava.io.File;publicclassMain{publicstaticvoidmain(String[] argv)throwsException{File dir =newFile("directoryName");String[] children = dir.list();if(children ==null){System.out.println("does not exist or
is not a directory");}else{for(int i =0; i < children.length; i++){String filename = children[i];System.out.println(filename);}}}}
importjava.io.File;importjava.io.IOException;importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[] args)throwsIOException{System.out.println("Enter the path to folder to search for files");Scanner s1 =newScanner(System.in);String folderPath = s1.next();File folder =newFile(folderPath);if(folder.isDirectory()){File[] listOfFiles = folder.listFiles();if(listOfFiles.length <1)System.out.println("There is no File inside Folder");elseSystem.out.println("List of Files & Folder");for(File file : listOfFiles){if(!file.isDirectory())System.out.println(
file.getCanonicalPath().toString());}}elseSystem.out .println("There is no Folder @ given path :"+ folderPath);}}
上面的代码示例将产生以下结果。
Enter the path to folder to search for files
C:/List of Files&FolderC:\bootmgr
C:\BOOTNXT
C:\hiberfil.sys
C:\pagefile.sys
C:\recovery.img.img
C:\swapfile.sys