1
<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?

user2040600
  • 125
  • 1
  • 4
  • 13
  • Possible duplicate: http://stackoverflow.com/questions/5751335/using-file-listfiles-with-filenameextensionfilter – vish213 May 02 '13 at 19:13

2 Answers2

3

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.

A4L
  • 17,353
  • 6
  • 49
  • 70
0
<%@ 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> <%}
%>
Shakya RDN
  • 21
  • 1
  • 6