Solved

Display directory content and sort by last-modified date, in JSP

Posted on 2004-08-02
8
1,435 Views
Last Modified: 2008-02-26
I am displaying the contents of a server directory on a JSP webpage. I want to sort the information and display in descending chronological order based on the "last modified" date. I am able to collect the names of the  contents ("files") and I have the last modified date in numeric form as a "long" variable, but I can't figure out how to sort it. Here is my code:


<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%
File dir = new File("/DATA/tomcat4/webapps/tf/DICOM");
String[] files = dir.list();

out.print("<p>The following Accession numbers have been received:</b></p>");

out.print("<table width=700><tr>");
      out.print("<td width=30><b>#</b></td>");
      out.print("<td width=150><b>Accession #</b></td>");
      out.print("<td width=300><b>Date Received</b></td>");
      out.print("<td width=220><b>Last Mod</b></td>");
      out.print("<tr><td colspan=4><hr></td></tr>");
out.print("</tr></table>");

for (int i = 0; i < files.length; i++) {

      String thepath = dir.getPath();
      thepath = thepath + "/";
      thepath = thepath + files[i];

      File f = new File(thepath);
      long lastmod = f.lastModified();

      java.util.Date d = new java.util.Date(lastmod);

      out.print("<table width=700><tr>");
            out.print("<td width=30><font size=\"2\">" + i + "</font></td>");
            out.print("<td width=150><a href=\"listseries.jsp?accession=" + files[i] + "\">" + files[i] + "</a></td>");
            out.print("<td width=300><font size=\"2\">" + d + "</font></td>");
            out.print("<td width=220>" + lastmod + "</td>");
      out.print("</tr></table>");
}
%>

Thank you for your help,
ak
0
Comment
Question by:akamauu
  • 6
  • 2
8 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 11694598
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>

<%
File dir = new File("/DATA/tomcat4/webapps/tf/DICOM");
String[] files = dir.list();

Arrays.sort( files, new Comparator()
                           {
                               public int compare(Object a, Object b)
                               {
                                   File filea = (File)a;
                                   File fileb = (File)b;
                                   return (int)( filea.getLastModified() - fileb.getLastModified() ) ;
                               }
                           } );

out.print("<p>The following Accession numbers have been received:</b></p>");

out.print("<table width=700><tr>");
     out.print("<td width=30><b>#</b></td>");
     out.print("<td width=150><b>Accession #</b></td>");
     out.print("<td width=300><b>Date Received</b></td>");
     out.print("<td width=220><b>Last Mod</b></td>");
     out.print("<tr><td colspan=4><hr></td></tr>");
out.print("</tr></table>");

for (int i = 0; i < files.length; i++) {

     String thepath = dir.getPath();
     thepath = thepath + "/";
     thepath = thepath + files[i];

     File f = new File(thepath);
     out.print("<table width=700><tr>");
          out.print("<td width=30><font size=\"2\">" + i + "</font></td>");
          out.print("<td width=150><a href=\"listseries.jsp?accession=" + files[i] + "\">" + files[i] + "</a></td>");
          out.print("<td width=300><font size=\"2\">" + d + "</font></td>");
          out.print("<td width=220>" + lastmod + "</td>");
     out.print("</tr></table>");
}
%>
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11694697
Oh hang on...  You get an array of names don't you?

Switch it to an array of files:

------------------

 <%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>

<%
File dir = new File("/DATA/tomcat4/webapps/tf/DICOM");
File[] files = dir.listFiles();

Arrays.sort( files, new Comparator()
                           {
                               public int compare(Object a, Object b)
                               {
                                   File filea = (File)a;
                                   File fileb = (File)b;
                                   return (int)( filea.lastModified() - fileb.lastModified() ) ;
                               }
                           } );

out.print("<p>The following Accession numbers have been received:</b></p>");

out.print("<table width=700><tr>");
     out.print("<td width=30><b>#</b></td>");
     out.print("<td width=150><b>Accession #</b></td>");
     out.print("<td width=300><b>Date Received</b></td>");
     out.print("<td width=220><b>Last Mod</b></td>");
     out.print("<tr><td colspan=4><hr></td></tr>");
out.print("</tr></table>");

for (int i = 0; i < files.length; i++)
{
     out.print("<table width=700><tr>");
          out.print("<td width=30><font size=\"2\">" + i + "</font></td>");
          out.print("<td width=150><a href=\"listseries.jsp?accession=" + java.net.URLEncoder.encode( files[i].getName() ) + "\">" + files[i].getName() + "</a></td>");
          out.print("<td width=300><font size=\"2\">" + new Date( files[ i ].lastModified() ) + "</font></td>");
          out.print("<td width=220>" + files[ i ].lastModified() + "</td>");
     out.print("</tr></table>");
}
%>
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11694769
I added URLEncoder, so if there are spaces, or "&" chars in the filename, it will encode them correctly for adding to the URL :-)
0
 

Author Comment

by:akamauu
ID: 11726703
Thank you for your response. I was out of town for the past couple of days.

I tried it exactly as your most recent example and it didn't work correctly. It displayed as follows:

======
The following Accession numbers have been received:
#      Accession #       Date Received                                          Last Mod
0      2059790              Fri Jul 16 18:54:27 MDT 2004                  1090025667000
1      2065363              Wed Jul 28 13:14:15 MDT 2004               1091042055000
2      2060506              Fri Jul 30 13:57:03 MDT 2004                  1091217423000
3      2075179              Mon Aug 02 18:39:06 MDT 2004             1091493546000
4      1458047              Mon Aug 02 18:51:05 MDT 2004             1091494265000
5      2080462              Mon Aug 02 18:56:54 MDT 2004             1091494614000
6      2079823              Mon Aug 02 20:21:44 MDT 2004             1091499704000
7      1032240              Mon Aug 02 21:02:21 MDT 2004             1091502141000
8      2076001              Mon Aug 02 21:03:59 MDT 2004             1091502239000
9      2009787              Tue Jun 29 16:20:03 MDT 2004                1088547603000
10    2052824              Wed Jul 14 14:38:14 MDT 2004               1089837494000
11    2082538              Fri Jul 30 16:13:12 MDT 2004                  1091225592000
12    2060509              Fri Jul 30 16:44:38 MDT 2004                  1091227478000
======

I'm not sure what I'm doing wrong.

P.S. thanks for the URLEncoder.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 35

Expert Comment

by:TimYates
ID: 11726776
Try this then:

File dir = new File("/DATA/tomcat4/webapps/tf/DICOM");
File[] files = dir.listFiles();

Arrays.sort( files, new Comparator()
                   {
                     public int compare(Object oa, Object ob)
                     {
                       long a = ((File)oa).lastModified() ;
                       long b = ((File)ob).lastModified() ;
                       return a < b ? -1 : a == b ? 0 : 1 ;
                     }
                   } );

out.print("<p>The following Accession numbers have been received:</b></p>");

out.print("<table width=700><tr>");
     out.print("<td width=30><b>#</b></td>");
     out.print("<td width=150><b>Accession #</b></td>");
     out.print("<td width=300><b>Date Received</b></td>");
     out.print("<td width=220><b>Last Mod</b></td>");
     out.print("<tr><td colspan=4><hr></td></tr>");
out.print("</tr></table>");

for (int i = 0; i < files.length; i++)
{
     out.print("<table width=700><tr>");
          out.print("<td width=30><font size=\"2\">" + i + "</font></td>");
          out.print("<td width=150><a href=\"listseries.jsp?accession=" + java.net.URLEncoder.encode( files[i].getName() ) + "\">" + files[i].getName() + "</a></td>");
          out.print("<td width=300><font size=\"2\">" + new Date( files[ i ].lastModified() ) + "</font></td>");
          out.print("<td width=220>" + files[ i ].lastModified() + "</td>");
     out.print("</tr></table>");
}
0
 
LVL 35

Accepted Solution

by:
TimYates earned 250 total points
ID: 11726780
that works on my machine :-)
0
 

Author Comment

by:akamauu
ID: 11727180
Thank you very much.  That is perfect. Also, I noticed that if I change "a < b" to "a > b" then I can display it in Reverse Chronological order.

Mahalo nui loa,
Aaron
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11727232
:-)  Yeah :-)

Good luck with it!!

Glad I could help :-)

Tim
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Help with LDAp login using jsp 6 109
unit test DAO layer 1 133
servlet cookie finding by name 1 60
if statement not resolving in my code 5 42
HOW TO: Upload an ISO image to a VMware datastore for use with VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere Host Client, and checking its MD5 checksum signature is correct.  It's a good idea to compare checksums, because many installat…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now