24

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?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
gkris
  • 1,209
  • 3
  • 17
  • 44
  • did you already printed out the tmp value and check if the file really exists? And if so, which env is this (windows or linux) in linux files are case sensitive... – Francisco Spaeth Jun 27 '12 at 06:42
  • yes I tried printing the temp value and the file indeed exists in the directory. It is windows OS, but the `file` parameter and the actual name of the file in the directory are case-sensitively same. – gkris Jun 27 '12 at 06:48

3 Answers3

53

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.

n0rm1e
  • 3,796
  • 2
  • 28
  • 37
5

This is where you have the bug.

String temp = file + ".MOD";

And

if(dir_contents[i].getName() == (file + ".MOD"))

The code boolean check = new File(temp).exists(); will check for the file in the current directory not in the required directory.

    String dirName="/home/demo";
    File dir = new File(dirName);
    File[] dir_contents = dir.listFiles();
    String temp = dirName+"/"+"README" + ".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().equals("README" + ".MOD"))
            return Constants.FILE_EXISTS;
            }

    return Constants.FILE_DOES_NOT_EXIST; 
Akhi
  • 2,242
  • 18
  • 24
  • @gkris and you have to use .equals() in the for loop – Akhi Jun 27 '12 at 06:54
  • for loop is just another method of checking the same file existence. I had 2 methods to check the same function because none were working. Now I can safely remove the whole for loop. Thanks again. – gkris Jun 27 '12 at 06:57
4

Try this..............

File f = new File("./file_name");
if(f.exists()){
    System.out.println("success");
}
else{
    System.out.println("fail");
}
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75