复制代码
- //:DirList.java
- //Displaysdirectorylisting
- packagec10;
- importjava.io.*;
- publicclassDirList{
- publicstaticvoidmain(String[]args){
- try{
- Filepath=newFile(".");
- String[]list;
- if(args.length==0)
- list=path.list();
- else
- list=path.list(newDirFilter(args[0]));
- for(inti=0;i<list.length;i++)
- System.out.println(list[i]);
- }catch(Exceptione){
- e.printStackTrace();
- }
- }
- }
- classDirFilterimplementsFilenameFilter{
- Stringafn;
- DirFilter(Stringafn){this.afn=afn;}
- publicbooleanaccept(Filedir,Stringname){
- //Strippathinformation:
- Stringf=newFile(name).getName();
- returnf.indexOf(afn)!=-1;
- }
- }///:~
复制代码
- //:DirList2.java
- //UsesJava1.1anonymousinnerclasses
- importjava.io.*;
- publicclassDirList2{
- publicstaticFilenameFilter
- filter(finalStringafn){
- //Creationofanonymousinnerclass:
- returnnewFilenameFilter(){
- Stringfn=afn;
- publicbooleanaccept(Filedir,Stringn){
- //Strippathinformation:
- Stringf=newFile(n).getName();
- returnf.indexOf(fn)!=-1;
- }
- };//Endofanonymousinnerclass
- }
- publicstaticvoidmain(String[]args){
- try{
- Filepath=newFile(".");
- String[]list;
- if(args.length==0)
- list=path.list();
- else
- list=path.list(filter(args[0]));
- for(inti=0;i<list.length;i++)
- System.out.println(list[i]);
- }catch(Exceptione){
- e.printStackTrace();
- }
- }
- }///:~
复制代码
- //:DirList3.java
- //Buildingtheanonymousinnerclass"in-place"
- importjava.io.*;
- publicclassDirList3{
- publicstaticvoidmain(finalString[]args){
- try{
- Filepath=newFile(".");
- String[]list;
- if(args.length==0)
- list=path.list();
- else
- list=path.list(
- newFilenameFilter(){
- publicboolean
- accept(Filedir,Stringn){
- Stringf=newFile(n).getName();
- returnf.indexOf(args[0])!=-1;
- }
- });
- for(inti=0;i<list.length;i++)
- System.out.println(list[i]);
- }catch(Exceptione){
- e.printStackTrace();
- }
- }
- }///:~
复制代码
- //:SortedDirList.java
- //Displayssorteddirectorylisting
- importjava.io.*;
- importc08.*;
- publicclassSortedDirList{
- privateFilepath;
- privateString[]list;
- publicSortedDirList(finalStringafn){
- path=newFile(".");
- if(afn==null)
- list=path.list();
- else
- list=path.list(
- newFilenameFilter(){
- publicboolean
- accept(Filedir,Stringn){
- Stringf=newFile(n).getName();
- returnf.indexOf(afn)!=-1;
- }
- });
- sort();
- }
- voidprint(){
- for(inti=0;i<list.length;i++)
- System.out.println(list[i]);
- }
- privatevoidsort(){
- StrSortVectorsv=newStrSortVector();
- for(inti=0;i<list.length;i++)
- sv.addElement(list[i]);
- //Thefirsttimeanelementispulledfrom
- //theStrSortVectorthelistissorted:
- for(inti=0;i<list.length;i++)
- list[i]=sv.elementAt(i);
- }
- //Testit:
- publicstaticvoidmain(String[]args){
- SortedDirListsd;
- if(args.length==0)
- sd=newSortedDirList(null);
- else
- sd=newSortedDirList(args[0]);
- sd.print();
- }
- }///:~
欢迎光临 仓酷云 (http://ckuyun.com/) | Powered by Discuz! X3.2 |