import java.io.FilenameFilter;
import java.io.File;
public class FileFilter implements FilenameFilter
{
private String _namePortion = "";
public boolean accept(File dir, String name)
{
return (name.startsWith(_namePortion));
}
public void setNamePortion(String value)
{
_namePortion = value;
}
}
import java.io.File;
public class FileFilter implements FilenameFilter
{
private String _namePortion = "";
public boolean accept(File dir, String name)
{
return (name.startsWith(_namePortion));
}
public void setNamePortion(String value)
{
_namePortion = value;
}
}
I added the setNamePortion() method to set the string that will be tested. Then in the servlet, I added the following code to find and delete the file(s) that met the criteria,
File directory = new File(uploadDir);
filter.setNamePortion(aString + "__");
File fileList[] = directory.listFiles(filter);
String filePathToDelete = "";
File f = null;
for (int i=0; i < fileList.length; i++)
{
filePathToDelete = fileList[i].getAbsolutePath();
f = new File(filePathToDelete);
f.delete();
filePathToDelete = "";
}
filter.setNamePortion(aString + "__");
File fileList[] = directory.listFiles(filter);
String filePathToDelete = "";
File f = null;
for (int i=0; i < fileList.length; i++)
{
filePathToDelete = fileList[i].getAbsolutePath();
f = new File(filePathToDelete);
f.delete();
filePathToDelete = "";
}
No comments:
Post a Comment