SoFunction
Updated on 2025-04-07

JSP programming method to obtain files, directories and windows drive letters in the current directory

This article describes the method of JSP programming to obtain files, directories and Windows drive letters in the current directory. Share it for your reference, as follows:

(I) Get the files and directories in the current directory

Knowledge points

1 Application of file object
2 listFiles() method
3 isDirectory() method, isFile() method determines whether it is a directory or a file
4 Convert string method toString

(1) Create a file object dir,
Then use the listFiles() method to return all files in the current directory

String path=("/");
File dir=new File(path);
File file[]=();

(2) Loop to determine whether the returned directory is a directory and display it:

<%
for(int i=0;i<;i++)
{
  if(file[i].isDirectory())//
  ("<br>"+file[i].toString());
}
%>

(3) Loop to determine whether the returned directory is a directory and display it:

<%
for(int i=0;i<;i++)
{
  if(file[i].isFile())
  ("<br>"+file[i].toString());
}
%>

program:

&lt;%@ page contentType="text/html;charset=GB2312"%&gt;
&lt;%@ page import=".*"%&gt;
&lt;html&gt;
&lt;title&gt;&lt;/title&gt;
&lt;body&gt;
&lt;%
String path=("/");
File dir=new File(path);
File file[]=();//Return the file in the specified directory using the listFiles() method of the file object%&gt;
current&lt;%=path%&gt;&lt;br&gt;
The directory has:
&lt;%
for(int i=0;i&lt;;i++)
{
  if(file[i].isDirectory())
  ("&lt;br&gt;"+file[i].toString());
}
%&gt;
&lt;p&gt;There are files:
&lt;%
for(int i=0;i&lt;;i++)
{
  if(file[i].isFile())
  ("&lt;br&gt;"+file[i].toString());
}
  %&gt;
&lt;/body&gt;
&lt;/html&gt;

(II) Get Windows Drive Letter

<%@ page contentType="text/html;charset=GB2312"%>
<%@ page import=".*"%>
<html>
<body>
<%
File[] roots=();
for(int i=0;i<;i++)
{
("<br>"+roots[i].getPath());
}
%>
</body>
</html>

(III) Merge the two programs

&lt;%@ page contentType="text/html;charset=GB2312"%&gt;
&lt;%@ page import=".*,.*,.*" %&gt;
&lt;%!
private final static int languageNo=0; //Language version, 0: Chinese; 1: EnglishString[] strDrivers   = {"Driver","Drivers"};
String[] strCurrentFolder= {"Current Directory","Current Folder"};
%&gt;
&lt;html&gt;
&lt;body&gt;
&lt;%
String path=("/");
File dir=new File(path);
File file[]=();//Return the file in the specified directory using the listFiles() method of the file objectStringBuffer sb=new StringBuffer(strDrivers[languageNo] + " : ");
File roots[]=();
for(int i=0;i&lt;;i++)
{
  (" &lt;a href=/"javascript:doForm('','"+roots[i]+"//','','','1','');/"&gt;");
  (roots[i]+"&lt;/a&gt;&amp;nbsp;");
}
//(sb);
%&gt;
&lt;%
//("&lt;table border='1' width='100%' bgcolor='#FFFFFF' cellspacing=0 cellpadding=5 bordercolorlight=#000000 bordercolordark=#FFFFFF&gt;&lt;tr&gt;&lt;td&gt;"+strCurrentFolder[languageNo]+":&lt;br&gt;&lt;b&gt;"+path+"&lt;/td&gt;&lt;td&gt;"+sb+"&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;");
%&gt;
&lt;table border='1' width='100%' bgcolor='#FFFFFF' cellspacing=0 cellpadding=5 bordercolorlight=#000000 bordercolordark=#FFFFFF&gt;
&lt;tr&gt;
&lt;td width='40%'&gt;&lt;%=strCurrentFolder[languageNo]%&gt;:&lt;br&gt;&lt;%=path %&gt;&lt;/td&gt;&lt;td width='60%'&gt;&lt;%=strDrivers[languageNo] %&gt;:&lt;%=sb%&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;table border='1' width='100%' bgcolor='#FFFFFF' cellspacing=0 cellpadding=5 bordercolorlight=#000000 bordercolordark=#FFFFFF&gt;
&lt;tr&gt;
&lt;td width='40%'&gt;The directory has:
&lt;%
for(int i=0;i&lt;;i++)
{
  if(file[i].isDirectory())
  ("&lt;br&gt;"+file[i].toString());
}
%&gt;
&lt;/td&gt;
&lt;td width='60%'&gt;There are files:
&lt;%
for(int i=0;i&lt;;i++)
{
  if(file[i].isFile())
  ("&lt;br&gt;"+file[i].toString());
}
  %&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;

We further optimize and achieve

&lt;%@ page contentType="text/html; charset=GBK" %&gt;
&lt;%@page import=".*,.*,.*" %&gt;
&lt;head&gt;
&lt;style type="text/css"&gt;
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 {
background: #FFFFFF;
}
#tabcontentcontainer{
width: 100%;
padding: 5px;
border: 1px solid black;
}
.tabcontent{
display:none;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;%!
private final static int languageNo=1; //Language version, 0: Chinese; 1: EnglishString[] strCurrentFolder= {"Current Directory","Current Folder"};
String[] strDrivers   = {"Driver","Drivers"};
String[] strFileName   = {"File Name","File Name"};
String[] strFileSize   = {"File Size","File Size"};
String[] strLastModified = {"Last Modification","Last Modified"};
String[] strFileOperation= {"File Operation","Operations"};
String[] strFileEdit   = {"Revise","Edit"};
String[] strFileDown   = {"download","Download"};
String[] strFileCopy   = {"copy","Move"};
String[] strFileDel   = {"delete","Delete"};
String getDrivers() 
{ 
StringBuffer sb=new StringBuffer(": "); 
File roots[]=(); 
for(int i=0;i&lt;;i++) 
{ 
("&lt;a href='?path="+roots[i]+"'&gt;"); 
(roots[i]+"&lt;/a&gt; &amp;nbsp;&amp;nbsp;"); 
} 
return (); 
}
String formatPath(String p)
{
StringBuffer sb=new StringBuffer();
for (int i = 0; i &lt; (); i++) 
{
  if((i)=='//')
  {
  ("////");
  }
  else
  {
  ((i));
  }
}
return ();
}
public static String toChinese(String str)
{
  try{
  if(str==null)
  {
   return "";
  }else
  {
   str=new String(("ISO8859_1"),"GBK");
   return str;
  }
  }catch(Exception e)
  {
  return "";
  }
}
%&gt;
&lt;%
("gb2312");
String strDir =toChinese( ("path")); 
if(strDir==null||()&lt;1)
{
strDir = ("/");
}
StringBuffer sb=new StringBuffer(""); 
StringBuffer sbFile=new StringBuffer(""); 
try 
{
File objFile = new File(strDir); 
File list[] = (); 
if(().length()&gt;3) 
{ 
("&lt;tr&gt;&lt;td &gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&lt;a href='?path="+formatPath(
().getAbsolutePath())+"'&gt;"); 
("To ParentFile&lt;/a&gt;&lt;br&gt;- - - - &lt;/td&gt;&lt;/tr&gt;/r/n"); 
} 
for(int i=0;i&lt;;i++) 
{ 
if(list[i].isDirectory()) 
{ 
("&lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;"); 
("&lt;a href='?path="+formatPath(list[i].getAbsolutePath())+"'&gt;"+list[i].getName()+"&lt;/a&gt;"); 
("&lt;/td&gt;&lt;/tr&gt;");
} 
else 
{ 
String strLen=""; 
String strDT=""; 
long lFile=0; 
lFile=list[i].length();
if(lFile&gt;1000000) 
{ 
lFile=lFile/1000000; 
strLen="" + lFile + " M"; 
} 
else if(lFile&gt;1000) 
{ 
lFile=lFile/1000; 
strLen="" + lFile + " K"; 
} 
else 
{ 
strLen="" + lFile + " Byte"; 
} 
Date dt=new Date(list[i].lastModified()); 
strDT=(); 
("&lt;tr&gt;&lt;td&gt;"); 
(""+list[i].getName()); 
("&lt;/td&gt;&lt;td&gt;"); 
(""+strLen); 
("&lt;/td&gt;&lt;td&gt;"); 
(""+strDT); 
("&lt;/td&gt;&lt;td&gt;");
(" &lt;a href=&gt;");
(strFileEdit[languageNo]+"&lt;/a&gt; ");
(" &lt;a href=&gt;");
(strFileDown[languageNo]+"&lt;/a&gt; ");
(" &lt;a href=&gt;");
(strFileCopy[languageNo]+"&lt;/a&gt; ");
(" &lt;a href=&gt;");
(strFileDel[languageNo]+"&lt;/a&gt; ");
("&lt;/td&gt;&lt;/tr&gt;/r/n"); 
} 
} 
} 
catch(Exception e) 
{ 
("&lt;font color=red&gt;error: "+()+"&lt;/font&gt;"); 
} 
%&gt;
&lt;table border='1' width='100%' bgcolor='#FFFFFF' cellspacing=0 cellpadding=5 bordercolorlight=#000000 bordercolordark=#FFFFFF&gt;
&lt;tr&gt;
&lt;td width='20%'&gt;&lt;%=strCurrentFolder[languageNo]%&gt;:&lt;br&gt;&lt;%=strDir %&gt;&lt;/td&gt;&lt;td width='70%'&gt;&lt;%=strDrivers[languageNo] %&gt;&lt;%=getDrivers()%&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;table border='1' width='100%' bgcolor='#FFFFFF' cellspacing=0 cellpadding=5 bordercolorlight=#000000 bordercolordark=#FFFFFF&gt;
&lt;tr&gt;
&lt;td width='20%'valign="top"&gt;
&lt;table &gt;
&lt;tr&gt;&lt;td&gt;&lt;%=sb%&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/td&gt;
&lt;td width='70%'align="center" valign="top"&gt;
&lt;table width="100%"border="1" cellspacing="1" cellpadding="4" bordercolorlight="#000000" bordercolordark="#FFFFFF"&gt;
&lt;tr&gt;
&lt;td width="26%" align="center"&gt;&lt;%=strFileName[languageNo] %&gt;&lt;/td&gt;
&lt;td width="19%" align="center"&gt;&lt;%=strFileSize[languageNo] %&gt;&lt;/td&gt;
&lt;td width="29%" align="center"&gt;&lt;%=strLastModified[languageNo] %&gt;&lt;/td&gt;
&lt;td width="26%" align="center"&gt;&lt;%=strFileOperation[languageNo] %&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td width="25%" align="center"&gt;&lt;%=sbFile %&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

I hope this article will be helpful to everyone's jsp programming.