<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.io.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
int fcount = 0, dcount = 0;
%>
<%
String path = request.getRealPath("");
File d = new File(path);
File list[] = d.listFiles();
out.println("<font color=#ff0000>"+path+"该目录下的文件:</font><br>");
for(int i=0; i<list.length; i++){
if(list[i].isFile()){
out.println(list[i].getName()+"<br>");
fcount++;
}
}
out.println("<font color=#ff0000>"+path+"该目录下的目录:</font><br>");
for(int i=0; i<list.length; i++){
if(list[i].isDirectory()){
out.println(list[i].getName()+"<br>");
dcount++;
}
}
%>
<hr>
<% out.println("<hr>");%>
<h3>the result:</h3>
<center>
file count:<%=fcount %><br>
directory count:<%=dcount %>
</center>
</body>
</html>
输出结果:
D:\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\webtest\该目录下的文件:
1.txt
2.txt
3.txt
action.jsp
applicationCount.jsp
browserfile.jsp
Buy.jsp
checklogin.jsp
checkMessage.jsp
city.html
CityInfo.jsp
date.jsp
email.jsp
forward.jsp
hello.txt
hobby.html
hobbyInfo.jsp
inputMessage.jsp
jsp_include.jsp
login.jsp
NewFile.jsp
NewFile1.jsp
onBuffer.jsp
Order.jsp
readCookie.jsp
readwritefile.jsp
redirect.jsp
refresh.jsp
register.html
register.jsp
requestInfo.jsp
responseCookie.jsp
responseCookie1.jsp
sendRedirect.jsp
sessionCounter.jsp
sessionData.jsp
sessionInfo.jsp
sessionLife.jsp
setContentType.jsp
showInfo.jsp
showmess.jsp
showMessage.jsp
ShowProducts.jsp
static.html
status1.jsp
success.jsp
success2.jsp
t.txt
test.jsp
text.txt
time.jsp
useBean.jsp
user.jsp
usingCookie.html
usingCookie.jsp
usingPlugin.jsp
usingSession.jsp
visit.jsp
web.xml
wordexcel.jsp
writeCookie.jsp
D:\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\webtest\该目录下的目录:
images
META-INF
WEB-INF
the result:
file count:61
directory count:3