Java files in the Usebean package (defined by itself, below the src folder)
Fileaccept.java
PackageUsebean;ImportJava.io.File;ImportJava.io.FilenameFilter; Public classFileacceptImplementsfilenamefilter{PrivateString Extendname; Public voidSetextendname (String s) { This. Extendname= "." +s; } @Override Public BooleanAccept (File dir, String name) {//TODO auto-generated Method Stub returnName.endswith (extendname); }}
browserfile.jsp
<%@ Page Language="Java"Import="java.util.*"pageencoding="UTF-8"%><% StringPath=Request.getcontextpath (); StringBasePath=Request.getscheme ()+ "://" +Request.getservername ()+ ":" +Request.getserverport ()+Path+ "/";%><%@page Import="java.io.*" %><Jsp:usebeanID= "Use"Scope= "page"class= "Usebean." Fileaccept "/><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML><Head><Basehref= "<%=basePath%>"><title>My JSP ' browserfile.jsp ' starting page</title></Head><Body> <Fontstyle= "Font-weight:bold;font-family:microsoft Yahei">File browsing</Font><BR> <% intFcount=0, DCount=0; Path=Request.getrealpath (""); File D=NewFile (path); File list[]=D.listfiles (); /* //return a specific file//Use is an object, equivalent to fileaccept use=Newfileaccept (); Use.setextendname ("JSP"); Stringlist[]=d.list (use); for(intI=0; I<List.length;i++) {out.print (List[i]+"<br>"); } */ /* Stringlist[]=d.list ();//returns all directories and filenames as strings for(intI=0; I<List.length;i++) {out.print (List[i]+"<br>"); } */ //output File Out.print (path+"files under the directory:"+"<br>"); for(intI=0; I<List.length;i++){ if(List[i].isfile ()) {Out.print (List[i].getname () )+"<br>"); Fcount++; } } //Output Directory Out.print ("<br>"+Path+"Directory under directory:"+"<br>"); for(intI=0; I<List.length;i++){ if(List[i].isdirectory ()) {Out.print (List[i].getname () )+"<br>"); DCount++; } } %> <H3style= "Color:blue">Statistical results:</H3> <Center>Total Files:<%=Fcount%><BR>Total Directories:<%=DCount%> </Center> </Body></HTML>
MyEclipse------traverse (all or specific) files and directories under a path