
案例八:目录列表的显示 教学目的: 拿漏帽对于文件H包括顺序读取文件和随机读取文件的操作方法: 设计要求: 读取各种计算机文件系统的文件列表。 案例副析 Fie类扩展了Object对象,实现了Serial女able,Comparable定义的接口,可以通过Ale 的一个实例对m上的文件系统进行各种慢作。 授定要测览的目录:调用s©tP方法,要注意操作系统为微软操作系统,每个路径分 隔符应写成两个斜杠: 移动列表记录,使用neFe方法用米。 可以通过getFieName()得到文件或文件夹名称, 获取文件尺寸:使用getRle5缸cO, 获取文件的最后修改时间:使用getfileTimeStamp) 判断是者是一个文作目录:使用g时FileType0: 该类通过:erator类将一个文件列表实现对列表的达代操作: 参考代码 文件列表功能实现:FleViewer.小va import java.io.File; import java.util.Date: import java.util.Iterator; import java.util.Vector; public c由ss FileViewer File myDir; Filel]contents; ector vectorList由 Iterator currentFleView:
案例八:目录列表的显示 教学目的: 掌握 java 对于文件(包括顺序读取文件和随机读取文件)的操作方法; 设计要求: 读取各种计算机文件系统的文件列表。 案例剖析 File 类扩展了 Object 对象,实现了 Serializable, Comparable 定义的接口,可以通过 File 的一个实例对 jvm 上的文件系统进行各种操作。 设定要浏览的目录:调用 setPath()方法,要注意操作系统为微软操作系统,每个路径分 隔符应写成两个斜杠\\); 移动列表记录:使用 nextFile()方法用来, 可以通过 getFileName()得到文件或文件夹名称, 获取文件尺寸:使用 getFileSize(), 获取文件的最后修改时间:使用 getFileTimeStamp() 判断是否是一个文件目录:使用 getFileType()。 该类通过 Iterator 类将一个文件列表实现对列表的迭代操作; 参考代码 文件列表功能实现:FileViewer.java import java.io.File; import java.util.Date; import java.util.Iterator; import java.util.Vector; public class FileViewer{ File myDir; File[] contents; Vector vectorList; Iterator currentFileView;

File currentRle; String path; public FileViewer()( path=new String("": vectorList-new Vector: 1 public FileViewer(String path)( hs.path=path水 vectorList-new VectorD: ∥设置测苋的路径 public void setPath(String path) th格,Path-path店 ∥返日当前目录塔径 public String getDirectory) return myDir.getPath0: ∥到新列表 public void refreshListO[ if(this-path.equals(""))path="c:\\"; myDir=new Filelpath); vectorList.dearl contents =myDir.listFiles0; ∥重新装入路径下文件 for[int i-0:icCONTENTS.LENGTH;I++K vectorList add(contents(i]
File currentFile; String path; public FileViewer(){ path=new String(""); vectorList=new Vector(); } public FileViewer(String path){ this.path=path; vectorList=new Vector(); } // 设置浏览的路径 public void setPath(String path){ this.path=path; } // 返回当前目录路径 public String getDirectory(){ return myDir.getPath(); } // 刷新列表 public void refreshList(){ if(this.path.equals("")) path="c:\\"; myDir=new File(path); vectorList.clear(); contents =myDir.listFiles(); //重新装入路径下文件 for(int i=0;i<CONTENTS.LENGTH;I++){ vectorList.add(contents[i]); }

currentFileview=vectorListiterator(): ·移动当前文件集合的指针指到下一个条口 ·@return成功返回true,否则false y public boolean nextFilel while(currentFileview.hasNext currentFile(FleurrentFileView.next0: return true; 1 return false; 1 ·赵网当前指向的文件对象的文件名称 1 public String getfileNamel) return currentFile-getName(); 1 ·返回当前指向的文件对象的文件尺寸 1 public String getfileSizel return new Long(currentFile.lengtho].toString0: 1 ·返回当前指向的文件对象的最后修政日期 / public String getFileTimeStamp(X return new Date(currentFile.lastModified0l.toString0:
currentFileView=vectorList.iterator(); } /** * 移动当前文件集合的指针指到下一个条目 * @return 成功返回 true,否则 false */ public boolean nextFile(){ while(currentFileView.hasNext()){ currentFile=(File)currentFileView.next(); return true; } return false; } /** * 返回当前指向的文件对象的文件名称 */ public String getFileName(){ return currentFile.getName(); } /** * 返回当前指向的文件对象的文件尺寸 */ public String getFileSize(){ return new Long(currentFile.length()).toString(); } /** * 返回当前指向的文件对象的最后修改日期 */ public String getFileTimeStamp(){ return new Date(currentFile.lastModified()).toString();

·返回当前指向的文件对象是否是一个文件目录 public boolean getFieTypel) return currentFile.is Directory() 1 测试类test.java import java.io." public c尚ss test public static void main(String[]argsH System.out.printin("File List"]: FileViewer f-new FileViewer(: isetPath('d:\): frefreshList() while(tnextFile())( System.out.print/f.getfileName)): if(lf.getFileType[) System.out.print"*+fgetFleSie(l else System.out.print(" "生 System.out.printif.getFileTimeStampl)"":
} /** * 返回当前指向的文件对象是否是一个文件目录 */ public boolean getFileType(){ return currentFile.isDirectory(); } } 测试类 test.java import java.io.*; public class test{ public static void main(String[] args){ System.out.println("File List"); FileViewer f=new FileViewer(); f.setPath("d:\\"); f.refreshList(); while(f.nextFile()){ System.out.print(f.getFileName()); if(!f.getFileType()) System.out.print(" "+f.getFileSize()); else System.out.print(" "); System.out.print(f.getFileTimeStamp()+"\n"); } } }