本文實例講述了jsp編程獲取當(dāng)前目錄下的文件和目錄及windows盤符的方法。分享給大家供大家參考,具體如下:
(一)獲取當(dāng)前目錄下的文件和目錄
知識點
1 file對象的應(yīng)用
2 listFiles()方法
3 isDirectory()方法,isFile()方法 判斷是否為目錄或是文件
4轉(zhuǎn)換字符串方法toString
(1)創(chuàng)建一個file對象dir,
然后用listFiles()方法返回當(dāng)前目錄下所有文件
String path=request.getRealPath("/");
File dir=new File(path);
File file[]=dir.listFiles();
(2)循環(huán)判斷所返回的是否為目錄,并顯示出來:
%
for(int i=0;ifile.length;i++)
{
if(file[i].isDirectory())//
out.print("br>"+file[i].toString());
}
%>
(3)循環(huán)判斷所返回的是否為目錄,并顯示出來:
%
for(int i=0;ifile.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()方法返回指定目錄下的文件
%>
當(dāng)前%=path%>br>
目錄有:
%
for(int i=0;ifile.length;i++)
{
if(file[i].isDirectory())
out.print("br>"+file[i].toString());
}
%>
p>文件有:
%
for(int i=0;ifile.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;iroots.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 = {"驅(qū)動器","Drivers"};
String[] strCurrentFolder= {"當(dāng)前目錄","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;iroots.length;i++)
{
sb.append(" a href=/"javascript:doForm('','"+roots[i]+"http://','','','1','');/">");
sb.append(roots[i]+"/a>nbsp;");
}
//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;ifile.length;i++)
{
if(file[i].isDirectory())
out.print("br>"+file[i].toString());
}
%>
/td>
td width='60%'>文件有:
%
for(int i=0;ifile.length;i++)
{
if(file[i].isFile())
out.print("br>"+file[i].toString());
}
%>
/td>
/tr>
/table>
/body>
/html>
我們進一步的優(yōu)化,實現(xiàn)
%@ 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= {"當(dāng)前目錄","Current Folder"};
String[] strDrivers = {"驅(qū)動器","Drivers"};
String[] strFileName = {"文件名稱","File Name"};
String[] strFileSize = {"文件大小","File Size"};
String[] strLastModified = {"最后修改","Last Modified"};
String[] strFileOperation= {"文件操作","Operations"};
String[] strFileEdit = {"修改","Edit"};
String[] strFileDown = {"下載","Download"};
String[] strFileCopy = {"復(fù)制","Move"};
String[] strFileDel = {"刪除","Delete"};
String getDrivers()
{
StringBuffer sb=new StringBuffer(": ");
File roots[]=File.listRoots();
for(int i=0;iroots.length;i++)
{
sb.append("a href='?path="+roots[i]+"'>");
sb.append(roots[i]+"/a> nbsp;nbsp;");
}
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("http:////");
}
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 >nbsp;/td>td>a href='?path="+formatPath(objFile.getParentFile
().getAbsolutePath())+"'>");
sb.append("To ParentFile/a>br>- - - - /td>/tr>/r/n");
}
for(int i=0;ilist.length;i++)
{
if(list[i].isDirectory())
{
sb.append("tr>td>nbsp;/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程序設(shè)計有所幫助。
您可能感興趣的文章:- Node.js fs模塊(文件模塊)創(chuàng)建、刪除目錄(文件)讀取寫入文件流的方法
- nodejs 遞歸拷貝、讀取目錄下所有文件和目錄
- js 獲取本地文件及目錄的方法(推薦)
- JavaScript獲取當(dāng)前運行腳本文件所在目錄的方法
- javascript讀取本地文件和目錄方法詳解