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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -