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) { (int = 0; < 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 lists files, want list files of specific extensions such .txt. can pl tell me how go this?
you need filenamefilter , implements method accept in such way accept file witch have extension need.
here sample code
new file("").list(new filenamefilter() { @override public boolean accept(file dir, string name) { return name.endswith(".txt"); } });
note code not case sensitive, files ending .txt
filtered out. may want extract extension , use equalsignorecase compare it. alternatively can lowercase name
before calling endswith.
Comments
Post a Comment