正在加载图片...
Directory Listing r Here's a sample recursive method for nonnumerical application The routine lists the names of all files in a given directory and its subdirectories public void directorylisting( File dir //assumption: dir represents a directory String[] filelist = dir. list()i//get the contents String dirPath getAbsolutePath( for (int i =0; i< fileList length 1++) File file new File( dirPath "\\" filelist[i] )i Tes if( file. isFile()( //it's a file End case h System. out. println( file. getName()) else i Recursive case directoryListing( file )i //it's a directory //so recurse C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 16-5© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 16 - 5 Directory Listing Here’s a sample recursive method for nonnumerical application. The routine lists the names of all files in a given directory and its subdirectories. public void directoryListing( File dir ) { //assumption: dir represents a directory String[] fileList = dir.list(); //get the contents String dirPath = dir.getAbsolutePath(); for (int i = 0; i < fileList.length; i++) { File file = new File( dirPath + "\\" + fileList[i] ); if ( file.isFile() ) { //it’s a file System.out.println( file.getName() ); } else { directoryListing( file ); //it’s a directory } //so recurse } } Recursive case End case Test
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有