Home>

(1) Get files and directories in the current directory

Knowledge point

1 Application of the file object

2 listfiles () method

3 isdirectory () method, isfile () method to determine whether it is a directory or a file

4 convert string method tostring

(1) Create a file object dir,

Then use listfiles () method to return all files in the current directory

string path=request.getrealpath ("/");
file dir=new file (path);
file file []=dir.listfiles ();

(2) circularly judge whether the returned is a directory,And it shows:

<%
for (int i=0;i<file.length;i ++)
{
  if (file [i] .isdirectory ()) //
  out.print ("<br>" + file [i] .tostring ());
}
%>

(3) circularly judge whether the returned is a directory,And it shows:

<%
for (int i=0;i<file.length;i ++)
{
  if (file [i] .isfile ())
  out.print ("<br>" + file [i] .tostring ());
}
%>

program:

<%@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 ();//The listfiles () method of the file object returns the files in the specified directory
%>
Current<%= path%><br>
The directories are:
<%
for (int i=0;i<file.length;i ++)
{
  if (file [i] .isdirectory ())
  out.print ("<br>" + file [i] .tostring ());
}
%>
<p>files are:
<%
for (int i=0;i<file.length;i ++)
{
  if (file [i] .isfile ())
  out.print ("<br>" + file [i] .tostring ());
}
  %>
</body>
</html>

(B) Get the Windows drive letter

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

(3) Merge two programs

<%@page contenttype="text/html;charset=gb2312"%>
<%@page import="java.io. *, Java.util. *, Java.net. *"%>
<%!
private final static int languageno=0;//language version,0:Chinese;1:English
string [] strdrivers={"Driver", "drivers"};
string [] strcurrentfolder={"current folder", "current folder"};
%>
<html>
<body>
<%
string path=request.getrealpath ("/");
file dir=new file (path);
file file []=dir.listfiles ();//The listfiles () method of the file object returns the files in the specified directory
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>
</table>
<table border="1" width="100%" bgcolor="#ffffff" cellspacing=0 cellpadding=5 bordercolorlight=#000000 bordercolordark=#ffffff>
<tr>
<td width="40%">The directories are:
<%
for (int i=0;i<file.length;i ++)
{
  if (file [i] .isdirectory ())
  out.print ("<br>" + file [i] .tostring ());
}
%>
</td>
<td width="60%">The files are:
<%
for (int i=0;i<file.length;i ++)
{
  if (file [i] .isfile ())
  out.print ("<br>" + file [i] .tostring ());
}
  %>
</td>
</tr>
</table>
</body>
</html>

Our further optimization,achieve

<%@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;//language version,0:Chinese;1:English
string [] strcurrentfolder={"current folder", "current folder"};
string [] strdrivers={"Driver", "drivers"};
string [] strfilename={"file name", "file name"};
string [] strfilesize={"file size", "file size"};
string [] strlastmodified={"last modified", "last modified"};
string [] strfileoperation={"File operations", "operations"};
string [] strfileedit={"modified", "edit"};
string [] strfiledown={"download", "download"};
string [] strfilecopy={"copy", "move"};
string [] strfiledel={"Delete", "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>
}
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
  • Previous Learn with me call (), apply (), bind () and callbacks in javascript
  • Next Explain related knowledge about object manipulation in JavaScript