Solved

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

Posted on 2004-08-02
8
1,501 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
report generation frameworks 1 70
Reading data  from excel sheet and displaying 1 92
spring JDBC Template example error 26 321
tomcat startup error 5 132
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
Arrow Electronics was searching for a KVM  (Keyboard/Video/Mouse) switch that could display on one single monitor the current status of all units being tested on the rack.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

685 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