aspose file tools
The moose likes I/O and Streams and the fly likes Check if a file exist in a directory without its extension Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Reply Bookmark "Check if a file exist in a directory without its extension" Watch "Check if a file exist in a directory without its extension" New topic
Author

Check if a file exist in a directory without its extension

tash ta
Greenhorn

Joined: Nov 24, 2011
Posts: 2
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

Joined: May 29, 2005
Posts: 468
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

Joined: Aug 05, 2005
Posts: 2686
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.


Joanne
tash ta
Greenhorn

Joined: Nov 24, 2011
Posts: 2
@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

Joined: Aug 05, 2005
Posts: 2686
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.
 
IntelliJ Java IDE
 
subject: Check if a file exist in a directory without its extension
 
Threads others viewed
charAt used on arraylist
reading character from a string array
Reading a file using NIO
Problem with Copying and Renaming FIle once it reaches its limit
Convert 1d array to 2D boolean
MyEclipse, The Clear Choice

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture