0

I'm simply trying to find whether an XML file exists or not. I have this code:

File f = new File("customers/jim.xml");
File g = new File("customers/jim.txt");

  if(f.exists())
      {
          System.out.println("File f exists!");
      }
      else
      {
          System.out.println("File f not found!");
      }

  if(g.exists())
      {
          System.out.println("File g exists!");
      }
      else
      {
          System.out.println("File g not found!");
      }

The output:

File f not found!
File g exists!

The text file is found, the xml one not. Both files are in the same folder, and the spelling is indeed correct. Anyone know what I'm doing wrong?

7
  • do you mind posting the contents, i.e. file names in customers directory? Commented Jun 12, 2013 at 12:30
  • 1
    You described what you think is correct, then we can hardly get anything wrong. More information needed, ls or dir result under that directory. Commented Jun 12, 2013 at 12:30
  • Whitespace in the file name? Which OS? Commented Jun 12, 2013 at 12:31
  • 1
    Also check that file suffix is not .XML Commented Jun 12, 2013 at 12:31
  • i would guess that the xml file doesnt exist, if you are using windows: does the explorer show extensions? or is there maybe an extension hidden behind the .xml. if you are using linux: is the character casing correct? Commented Jun 12, 2013 at 12:32

1 Answer 1

3

Everything looks correct so a few things to check:

  • Capital letters in the extension.
  • Hidden extra extension (jim.xml.txt but .txt hidden)
  • Do you have several customers directories? If so, your application might be looking in another one than you are expecting.
Sign up to request clarification or add additional context in comments.

2 Comments

+1 - Another possibility is that the jim.xml entry is a broken symlink or shortcut.
Hidden extra extension (jim.xml.txt but .txt hidden) Was the correct answer! I feel pretty silly right now. I will leave this thread here as a monument to me goofing up, and so that others may avoid making the same mistake. Feel free to lock this thread.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.