• Post Reply Bookmark Topic Watch Topic
  • New Topic

Check if a file exist in a directory without its extension

 
tash ta
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know how to check if a file exists without typing in th eextension of teh file in the FileDialog...and if the file does not exist a message should be displayed


FileDialog fileWindow = new FileDialog(new Frame(),"Tash & David's File Founder System");

fileWindow.setVisible(true);


if (fileWindow.getFile() != null ) {

File fileDirectory = new File(fileWindow.getDirectory());




String[] Directory = fileDirectory.list();


for (int i =0; i < Directory.length; i++) {



if(Directory[i].startsWith(fileWindow.getFile())){

int dot = Directory[i].lastIndexOf(".");
String file = Directory[i].substring(dot);

for(int x = 0; x < Directory[i].length()-file.length(); x++)

{
//System.out.print(Directory[i].length() + " " + file.length());
if(fileWindow.getFile().charAt(x) == Directory[i].charAt(x) )
{
System.out.print("found");
break;
}

}



}
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a question, or a problem? Does this code run, or were you expecting us to load it up, compile it, and run it, and then guess as to what you were asking?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a general point, you shouldn't list all the files in the directory and then check each of them afterwards. It is more normal to provide a FilenameFilter to the list method or a FileFilter to the listFiles method.
 
tash ta
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@RalphCook The code does run I jus want to know how to check if the exist without its extension within the directory
@JoanneNeal I do not know how to use FileFilterName at all
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tash ta wrote:@JoanneNeal I do not know how to use FileFilterName at all

Follow the links in my previous post. FilenameFilter is an interface that has an accept method with two parameters - the name of the directory and the name of the file.
Create a class that implements the FilenameFilter interface and has a constructor that accepts the name of the file you are looking for as a parameter.
In the accept method, use the String.substring method to remove the extension from the name parameter. Then use String.equals (or equalsIgnorecase if your OS is not case sensitive) to compare the name without an extension to the name of the file you are looking for (which you passed in as a parameter to the constructor).
If you then call the File.list method and pass an instance of your class as a parameter, the accept method will be called for every file in that directory but only those that cause accept to return true will be included in the array returned from the list method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!