Solved

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

Posted on 2004-08-02
8
1,452 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Converting a Hibernate Query to use parameters 5 105
xjc and jaxb 2 90
Display the newly inserted values 3 74
Market Place not working in Eclipse Luna 2 101
Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

912 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

24 Experts available now in Live!

Get 1:1 Help Now