Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-08-02
8
Medium Priority
?
1,582 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Files go missing when using DFS (Distributed File System) Replication and how to recover them and fix it.
Are you an Exchange administrator employed with an organization? And, have you encountered a corrupt Exchange database due to which you are not able to open its EDB file. This article will explain all the steps to repair corrupt Exchange database.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

715 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