Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

selecting file types 1

Status
Not open for further replies.

yama

Programmer
Jun 28, 2001
69
SG
Hi, I am writing a jsp page for retrieving all the files in a particular, i can retrieve all the files, however is there any way to display the string of all the same file type?
thanks!
 
I am not sure I understand your problem. Can you describe it in more detail? Wushutwist
 
hmm... for example, i got a list.jsp, the list.jsp will display every file found in a particular directory of my hard drive, but what i want is to retrieve all files of the same file type, for example, *.txt. is there any way?
 
Use the following method from the File Class:
Code:
public String[] list(FilenameFilter filter)


Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter. The behavior of this method is the same as that of the list() method, except that the strings in the returned array must satisfy the filter. If the given filter is null then all names are accepted. Otherwise, a name satisfies the filter if and only if the value true results when the FilenameFilter.accept(java.io.File, java.lang.String) method of the filter is invoked on this abstract pathname and the name of a file or directory in the directory that it denotes.

Parameters:

filter - A filename filter

Returns:

An array of strings naming the files and directories in the directory denoted by this abstract pathname that were accepted by the given filter. The array will be empty if the directory is empty or if no names were accepted by the filter. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

Throws:

SecurityException - If a security manager exists and its SecurityManager.checkRead(java.io.FileDescriptor) method denies read access to the directory

You can create the following Filter to limit the files returned to *.txt. Create a DirectoryFilter.java file with the following source:
Code:
import java.util.*;

public class DirectoryFilter implements java.io.FilenameFilter {
  
  /* List of Strings */
  private List acceptedFileExtension;
  
  /* Accepts a list of Strings for file extensions */
  DirectoryFilter(List acceptedFileExtension) {
    this.acceptedFileExtension = acceptedFileExtension;
  }

  public boolean accept(File dir, String name) {
    /* Early out if name is null */
    if (name == null) { 
       return false; 
    }
    String fileExtension = getFileExtension(name);
    if (fileExtension != null && acceptedFileExtension.contains(fileExtension)) { 
       return true;
    }
    else { 
       return false; 
    }
  }
  
  private String getFileExtension(String name) {
    int index = name.lastIndexOf('.');
    if (index != -1) { 
       return name.substring(index+1).trim();
    }
    else { 
       return null; 
    }  
  }
}

Then your code would be:
Code:
/* Create File Object */
String path = "Your Directory Path";
File directory = new File(path);

/* Create DirectoryFilter */
ArrayList fileTypes = new ArrayList(1); //initial capacity 1
fileTypes.add("txt");
DirectoryFilter fileExtensionFilter = new DirectoryFilter(fileTypes);

/* Get Filtered Directory Listing */
String fileList[] = directory.list(fileExtensionFilter);

I haven't tested this but I am pretty sure it will work. You may need to make minor modifications to get it to compile (hopefully not). You could also do the same thing using anonymous classes. Let me know how it goes.
Wushutwist
 
hmm...thanks alot for ur effort =), but is there any other way to write the above codes using purely jsp codes?
thanks!
 
You can use your choice of Inner or Anonymous classes. I would perfer an Inner class in this case because it is more than a few lines of code. Try the following, again I haven't tested it.
Code:
<%@ page import=&quot;java.util.*, java.io.FilenameFilter&quot; %>

<%!
FilenameFilter createDirectoryFilter(final List FileTypes) {
  /* BEGIN INNER CLASS DEFINITION */
  class DirectoryFilter implements FilenameFilter {
    List acceptedFileExtension = FileTypes;
  
    public boolean accept(File dir, String name) {
      /* Early out if name is null */
      if (name == null) { 
        return false; 
      }
      String fileExtension = getFileExtension(name);
      if (fileExtension != null && acceptedFileExtension.contains(fileExtension)) { 
        return true;
      }
      else { 
        return false; 
      }
    }
  
    private String getFileExtension(String name) {
      int index = name.lastIndexOf('.');
      if (index != -1) { 
        return name.substring(index+1).trim();
      }
      else { 
        return null; 
      }  
    }
  } /* END OF INNER CLASS DEFINITION */
  
  return new DirectoryFilter();
}

%>

DO JSP/HTML STUFF

Code:
<%
/* Create File Object */
String path = &quot;Your Directory Path&quot;;
File directory = new File(path);

/* Create DirectoryFilter */
ArrayList fileTypes = new ArrayList(1); //initial capacity 1
fileTypes.add(&quot;txt&quot;);
FilenameFilter fileExtensionFilter = createDirectoryFilter(fileTypes);

/* Get Filtered Directory Listing */
String fileList[] = directory.list(fileExtensionFilter);
%>

DISPLAY ALL YOUR FILES AND FINISH YOUR JSP/HTML STUFF Wushutwist
 
sorry for the trouble, Thanks alot! =)
 
Hey, I was bored at work (-:. Let me know if you get that JSP working. Wushutwist
 
hmm... when i run them, the folloing errors comes out

org.apache.jasper.JasperException: Unable to compile class for JSPC:\WINDOWS\Desktop\Test\work\localhost_8080\_0002fsrc_0002ftest_00031_0002ejsptest1_jsp_45.java:25: local class DirectoryFilter (src._0002fsrc_0002ftest_00031_0002ejsptest1_jsp_45. 1$DirectoryFilter) must be declared abstract. It does not define boolean accept(java.io.File, java.lang.String) from interface java.io.FilenameFilter.
class DirectoryFilter implements FilenameFilter {
^
C:\WINDOWS\Desktop\Test\work\localhost_8080\_0002fsrc_0002ftest_00031_0002ejsptest1_jsp_45.java:28: Class src.File not found.
public boolean accept(File dir, String name) {
^
C:\WINDOWS\Desktop\Test\work\localhost_8080\_0002fsrc_0002ftest_00031_0002ejsptest1_jsp_45.java:53: local class DirectoryFilter (src._0002fsrc_0002ftest_00031_0002ejsptest1_jsp_45. 1$DirectoryFilter) is an abstract class. It can't be instantiated.
return new DirectoryFilter();
^
C:\WINDOWS\Desktop\Test\work\localhost_8080\_0002fsrc_0002ftest_00031_0002ejsptest1_jsp_45.java:105: Class src.File not found.
File directory = new File(path);
^
C:\WINDOWS\Desktop\Test\work\localhost_8080\_0002fsrc_0002ftest_00031_0002ejsptest1_jsp_45.java:105: Class src.File not found.
File directory = new File(path);
 
oh...i think i done it already
i add another page import
<%@ page import=&quot;java.io.*&quot;%>

thanks anyway! =)
 
Oh, sorry I overlooked the fact that I was using a File object later in the JSP. Wushutwist
 
its ok :), u have help me alot in this area, thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top