File类 浏览目录下的文件

<%@ 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 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值