user2040600
user2040600

Reputation: 125

list files of a specific extension in a folder using JSP

<h1>Directories</h1>
<ul>
<%
String root="c:/Repository/WebApplication/mydocs/javadoc/";
java.io.File file;
java.io.File dir = new java.io.File(root);

String[] list = dir.list();

if (list.length > 0) {

for (int i = 0; i < list.length; i++) {
file = new java.io.File(root + list[i]);
if (file.isDirectory()) {
%>
<li><a href="javadoc/<%=list[i]%>" target="_top"><%=list[i]%></a><br>
<%
 }
}
}
%>
</ul>

The above code works, i.e it lists all the files, I want to list only files of specific extensions such as .txt. Can anyone pl tell me how to go about this?

Upvotes: 1

Views: 5396

Answers (2)

Shakya RDN
Shakya RDN

Reputation: 21

<%@ page import="java.io.*" %>
<% 
    String file = application.getRealPath("/results");
    File f = new File(file);
    String [] fileNames = f.list();
    int i = 0;
    String fname=null;
    File [] fileObjects= f.listFiles();
    BufferedReader readReport;
    int num=0;

    {
        %>
        <table name="reports">
        <th width=12.5% align="center" bgcolor="gray">Execution ID</th>
        <th width=12.5% align="center" bgcolor="gray">Parent suite name</th>
        <th width=12.5% align="center" bgcolor="gray">Execution date</th>
        <th width=12.5% align="center" bgcolor="gray">Total execution time(seconds)</th>
        <th width=12.5% align="center" bgcolor="gray">Pass</th>
        <th width=12.5% align="center" bgcolor="gray">Fail</th>
        <th width=12.5% align="center" bgcolor="gray">Skip</th>
        <th width=12.5% align="center" bgcolor="gray">Summary</th>
        <%
    }

    for (i=0; i < fileObjects.length; i++)
    {

        if(!fileObjects[i].isDirectory())
            {
            fname = "../results/"+fileNames[i];

            if(fname.endsWith(".html"))
            {
                String Name = fileNames[i].substring(0, fileNames[i].indexOf('.'));


                    { 
                        %>
                        <tr bgcolor="lightgray">
                            <td width=12.5% align="center">
                                <%=Name%>
                            </td>

                            <td width=12.5% align="center">

                            </td>

                            <td width=12.5%  align="center">

                            </td>

                            <td width=12.5%  align="center">

                            </td>

                            <td width=12.5%  align="center">

                            </td>

                            <td width=12.5%  align="center">

                            </td>

                            <td width=12.5%  align="center">

                            </td> 

                            <td width=12.5%  align="center">
                                <a HREF="<%= fname %>" target="loadReport"><button>View</button></a>
                            </td>
                        </tr>

                        <%
                    }
                }
        }
    }
    {%></table> <%}
%>

Upvotes: 0

A4L
A4L

Reputation: 17595

You need a FilenameFilter and implements it method accept in such a way that you accept only file witch have the extension you need.

Here is a sample code

new File("").list(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.endsWith(".txt");
        }
    });

Note that this code is not case sensitive, so files ending with .TXT will be filtered out. You may want to extract the extension and then use equalsIgnoreCase to compare it. Alternatively you can LowerCase name before calling endsWith.

Upvotes: 3

Related Questions