I am trying to check for a specific file in a given directory. I don't want the code but I want to fix the one I have. The only difference in this question, is that I look for files with an extension .MOD
.
I have the code ready:-
public static int checkExists(String directory, String file) {
File dir = new File(directory);
File[] dir_contents = dir.listFiles();
String temp = file + ".MOD";
boolean check = new File(temp).exists();
System.out.println("Check"+check); // -->always says false
for(int i = 0; i<dir_contents.length;i++) {
if(dir_contents[i].getName() == (file + ".MOD"))
return Constants.FILE_EXISTS;
}
return Constants.FILE_DOES_NOT_EXIST;
}
But for some reasons, it does not work. I don't understand why, can anybody find any bug here?
Do you expect temp.MOD file to be in the current directory (the directory from which you run your application), or you want it to be in the "directory" folder? In the latter case, try creating the file this way:
boolean check = new File(directory, temp).exists();
Also check for the file permissions, because it will fail on permission errors as well. Case sensitivily might also be the cause of the issue as Spaeth mentioned.