Today we are going to learn about how to retrieve the created time and date of all files in a folder.
This is useful when you want to track the session information of certain files or folders.Lets look the code below.


<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.io.File, java.util.Date, java.util.Calendar" errorPage="" %>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<%
  String dir_name = "C:\\foldername";
  //String dir_name = "/";  This line will give u the windows drive's root folder.

  File fold = new File(dir_name);    

  if(fold.exists())
  {
      String all_files[] = fold.list();
     int no_of_files = all_files.length;

     for(int i=0; i<no_of_files; i++)
      {
        File f = new File(dir_name + "\\" + all_files[i] );
        String complete_file_name = f.getPath();
        String file_name = f.getName();
        long last_modified_ms = f.lastModified();
        Date date = new Date(last_modified_ms);
        Calendar cal_date = Calendar.getInstance();
        cal_date.setTime(date);
        String last_modified_date = cal_date.get(Calendar.MONTH) + "-" + cal_date.get(Calendar.DATE) +"-"+ cal_date.get(Calendar.YEAR);
        String last_modified_time = cal_date.get(Calendar.HOUR) + ":" + cal_date.get(Calendar.MINUTE) +":"+ cal_date.get(Calendar.SECOND);
%>       

        <a href="<%=complete_file_name%>">  <%=file_name%>  </a>   <b>Last Modified by </b> <%=last_modified_date%>      <b>at </b> <%=last_modified_time%>
        <br>   

<%

      }//end of for

  }//end of if
  else
     out.println("Folder does't exist");
%>
  <body>
</body>
</html>

Try this example and do comment us Happy coding.

Related Posts

  1. Print current time using java in jsp page
  2. Display current time and date in your Jsp page
  3. create a file from form input in java,jsp
  4. In Tomcat jsp page is not refreshing