Solved

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

Posted on 2004-08-02
8
1,543 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

This article describes how to import Lotus Notes Contacts into Outlook 2016, 2013, 2010 and 2007 etc. with a few manual steps. You can easily export and migrate Lotus Notes contacts into Microsoft Outlook without having to use any third party tools.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

622 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