【问题标题】:list files of a specific extension in a folder using JSP使用 JSP 列出文件夹中特定扩展名的文件
【发布时间】:2013-04-27 01:46:48
【问题描述】:
<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>

上面的代码有效,即它列出了所有文件,我只想列出特定扩展名的文件,例如 .txt。谁能告诉我该怎么做?

【问题讨论】:

标签: file jsp


【解决方案1】:

您需要一个FilenameFilter 并实现它的方法accept,这样您只接受具有您需要的扩展名的文件。

这是一个示例代码

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

请注意,此代码不区分大小写,因此以.TXT 结尾的文件将被过滤掉。您可能想要提取扩展名,然后使用equalsIgnoreCase 进行比较。或者,您可以在拨打endsWith 之前先LowerCase name

【讨论】:

    【解决方案2】:
    <%@ 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> <%}
    %>
    

    【讨论】:

    • 使用 endswith() 函数 :)
    • 这不是比需要的代码多吗?桌子与其余的有什么关系?另外,在另一个答案中已经建议使用endsWith
    猜你喜欢
    • 1970-01-01
    • 2011-03-10
    • 2016-11-16
    • 1970-01-01
    • 2014-04-10
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 2010-12-21
    相关资源
    最近更新 更多