本文執行個體講述了jsp編程擷取目前的目錄下的檔案和目錄及windows盤符的方法。分享給大家供大家參考,具體如下:
(一)擷取目前的目錄下的檔案和目錄
知識點
1 file對象的應用
2 listFiles()方法
3 isDirectory()方法,isFile()方法 判斷是否為目錄或是檔案
4轉換字串方法toString
(1)建立一個file對象dir,
然後用listFiles()方法返回目前的目錄下所有檔案
String path=request.getRealPath("/");File dir=new File(path);File file[]=dir.listFiles();
(2)迴圈判斷所返回的是否為目錄,並顯示出來:
<%for(int i=0;i<file.length;i++){ if(file[i].isDirectory())// out.print("<br>"+file[i].toString());}%>
(3)迴圈判斷所返回的是否為目錄,並顯示出來:
<%for(int i=0;i<file.length;i++){ if(file[i].isFile()) out.print("<br>"+file[i].toString());}%>
程式:
<%@ page contentType="text/html;charset=GB2312"%><%@ page import="java.io.*"%><html><title></title><body><%String path=request.getRealPath("/");File dir=new File(path);File file[]=dir.listFiles();//用file對象的listFiles()方法返回指定目錄下的檔案%>當前<%=path%><br>目錄有:<%for(int i=0;i<file.length;i++){ if(file[i].isDirectory()) out.print("<br>"+file[i].toString());}%><p>檔案有:<%for(int i=0;i<file.length;i++){ if(file[i].isFile()) out.print("<br>"+file[i].toString());} %></body></html>
(二)擷取windows盤符
<%@ page contentType="text/html;charset=GB2312"%><%@ page import="java.io.*"%><html><body><%File[] roots=File.listRoots();for(int i=0;i<roots.length;i++){out.print("<br>"+roots[i].getPath());}%></body></html>
(三)將兩個程式合并
<%@ page contentType="text/html;charset=GB2312"%><%@ page import="java.io.*,java.util.*,java.net.*" %><%!private final static int languageNo=0; //語言版本,0 : 中文; 1:英文String[] strDrivers = {"磁碟機","Drivers"};String[] strCurrentFolder= {"目前的目錄","Current Folder"};%><html><body><%String path=request.getRealPath("/");File dir=new File(path);File file[]=dir.listFiles();//用file對象的listFiles()方法返回指定目錄下的檔案StringBuffer sb=new StringBuffer(strDrivers[languageNo] + " : ");File roots[]=File.listRoots();for(int i=0;i<roots.length;i++){ sb.append(" <a href=/"javascript:doForm('','"+roots[i]+"//','','','1','');/">"); sb.append(roots[i]+"</a> ");}//out.println(sb);%><%//out.print("<table border='1' width='100%' bgcolor='#FFFFFF' cellspacing=0 cellpadding=5 bordercolorlight=#000000 bordercolordark=#FFFFFF><tr><td>"+strCurrentFolder[languageNo]+":<br><b>"+path+"</td><td>"+sb+"</td></tr></table>");%><table border='1' width='100%' bgcolor='#FFFFFF' cellspacing=0 cellpadding=5 bordercolorlight=#000000 bordercolordark=#FFFFFF><tr><td width='40%'><%=strCurrentFolder[languageNo]%>:<br><%=path %></td><td width='60%'><%=strDrivers[languageNo] %>:<%=sb%></td></tr></table><table border='1' width='100%' bgcolor='#FFFFFF' cellspacing=0 cellpadding=5 bordercolorlight=#000000 bordercolordark=#FFFFFF><tr><td width='40%'>目錄有:<%for(int i=0;i<file.length;i++){ if(file[i].isDirectory()) out.print("<br>"+file[i].toString());}%></td><td width='60%'>檔案有:<%for(int i=0;i<file.length;i++){ if(file[i].isFile()) out.print("<br>"+file[i].toString());} %></td></tr></table></body></html>
我們進一步的最佳化,實現
<%@ page contentType="text/html; charset=GBK" %><%@page import="java.io.*,java.util.*,java.net.*" %><head><style type="text/css">td,select,input,body{font-size:9pt;}A { text-decoration: none }#tablist{padding: 5px 0;margin-left: 0;margin-bottom: 0;margin-top: 0.1em;font:9pt;}#tablist li{list-style: none;display: inline;margin: 0;}#tablist li a{padding: 3px 0.5em;margin-left: 3px;border: 1px solid ;background: #FFFFFF;}#tablist li a:link, #tablist li a:visited{color: navy;}#tablist li a.current{background: #FFFFFF;}#tabcontentcontainer{width: 100%;padding: 5px;border: 1px solid black;}.tabcontent{display:none;}</style></head><%!private final static int languageNo=1; //語言版本,0 : 中文; 1:英文String[] strCurrentFolder= {"目前的目錄","Current Folder"};String[] strDrivers = {"磁碟機","Drivers"};String[] strFileName = {"檔案名稱","File Name"};String[] strFileSize = {"檔案大小","File Size"};String[] strLastModified = {"最後修改","Last Modified"};String[] strFileOperation= {"檔案操作","Operations"};String[] strFileEdit = {"修改","Edit"};String[] strFileDown = {"下載","Download"};String[] strFileCopy = {"複製","Move"};String[] strFileDel = {"刪除","Delete"};String getDrivers() { StringBuffer sb=new StringBuffer(": "); File roots[]=File.listRoots(); for(int i=0;i<roots.length;i++) { sb.append("<a href='?path="+roots[i]+"'>"); sb.append(roots[i]+"</a> "); } return sb.toString(); }String formatPath(String p){StringBuffer sb=new StringBuffer();for (int i = 0; i < p.length(); i++) { if(p.charAt(i)=='//') { sb.append("////"); } else { sb.append(p.charAt(i)); }}return sb.toString();}public static String toChinese(String str){ try{ if(str==null) { return ""; }else { str=new String(str.getBytes("ISO8859_1"),"GBK"); return str; } }catch(Exception e) { return ""; }}%><%request.setCharacterEncoding("gb2312");String strDir =toChinese( request.getParameter("path")); if(strDir==null||strDir.length()<1){strDir = request.getRealPath("/");}StringBuffer sb=new StringBuffer(""); StringBuffer sbFile=new StringBuffer(""); try {File objFile = new File(strDir); File list[] = objFile.listFiles(); if(objFile.getAbsolutePath().length()>3) { sb.append("<tr><td > </td><td><a href='?path="+formatPath(objFile.getParentFile().getAbsolutePath())+"'>"); sb.append("To ParentFile</a><br>- - - - </td></tr>/r/n"); } for(int i=0;i<list.length;i++) { if(list[i].isDirectory()) { sb.append("<tr><td> </td><td>"); sb.append("<a href='?path="+formatPath(list[i].getAbsolutePath())+"'>"+list[i].getName()+"</a>"); sb.append("</td></tr>");} else { String strLen=""; String strDT=""; long lFile=0; lFile=list[i].length();if(lFile>1000000) { lFile=lFile/1000000; strLen="" + lFile + " M"; } else if(lFile>1000) { lFile=lFile/1000; strLen="" + lFile + " K"; } else { strLen="" + lFile + " Byte"; } Date dt=new Date(list[i].lastModified()); strDT=dt.toLocaleString(); sbFile.append("<tr><td>"); sbFile.append(""+list[i].getName()); sbFile.append("</td><td>"); sbFile.append(""+strLen); sbFile.append("</td><td>"); sbFile.append(""+strDT); sbFile.append("</td><td>");sbFile.append(" <a href=>");sbFile.append(strFileEdit[languageNo]+"</a> ");sbFile.append(" <a href=>");sbFile.append(strFileDown[languageNo]+"</a> ");sbFile.append(" <a href=>");sbFile.append(strFileCopy[languageNo]+"</a> ");sbFile.append(" <a href=>");sbFile.append(strFileDel[languageNo]+"</a> ");sbFile.append("</td></tr>/r/n"); } } } catch(Exception e) { out.println("<font color=red>error: "+e.toString()+"</font>"); } %><table border='1' width='100%' bgcolor='#FFFFFF' cellspacing=0 cellpadding=5 bordercolorlight=#000000 bordercolordark=#FFFFFF><tr><td width='20%'><%=strCurrentFolder[languageNo]%>:<br><%=strDir %></td><td width='70%'><%=strDrivers[languageNo] %><%=getDrivers()%></td></tr></table><table border='1' width='100%' bgcolor='#FFFFFF' cellspacing=0 cellpadding=5 bordercolorlight=#000000 bordercolordark=#FFFFFF><tr><td width='20%'valign="top"><table ><tr><td><%=sb%></td></tr></table></td><td width='70%'align="center" valign="top"><table width="100%"border="1" cellspacing="1" cellpadding="4" bordercolorlight="#000000" bordercolordark="#FFFFFF"><tr><td width="26%" align="center"><%=strFileName[languageNo] %></td><td width="19%" align="center"><%=strFileSize[languageNo] %></td><td width="29%" align="center"><%=strLastModified[languageNo] %></td><td width="26%" align="center"><%=strFileOperation[languageNo] %></td></tr><tr><td width="25%" align="center"><%=sbFile %></td></tr></table></td></tr></table>
希望本文所述對大家jsp程式設計有所協助。