Tal Angel
Tal Angel

Reputation: 1762

The system cannot find the file specified but file exists

I'm trying to manipulate my XML file called Test.XML.

I can see the file in my folder and I can open it. Code:

DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();            
domFactory.setIgnoringComments(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(new File("MyFolder\Test.xml"));

I am getting this error:

java.io.FileNotFoundException: C:\MyFolder\Test.xml (The system cannot find the file specified)

Why can't the code open/read my file, but other programs like Notepad++ can do so?

***Note: the real name of the file is "Use-cases\testSuitesA_E_1002+${user}3_12022016+${date}2_2.5.xml".

Upvotes: 2

Views: 4447

Answers (7)

Anish B.
Anish B.

Reputation: 16439

Please modify your code to this :

ClassLoader classLoader = ClassLoader.getSystemClassLoader();
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setIgnoringComments(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(new File(classLoader.getResource("MyFolder/Test.xml").getPath()));
System.out.println(doc.getDocumentElement());

For this code to run, build the project for .class files. Classloader needs to have .class files. Otherwise, it will not able to read folder or files from classpath.

Note :

  • new File("MyFolder\Test.xml") - This will not work because you have not provided the absolute path. You have to use classloader to get file from classpath (in that case, you don't have to mention the full path). Classloader brings the full absolute path for you. Remember: java.nio.File needs absolute path for its working.

  • If you want to read file from any arbitrary location, then you have to specify the full path for that. (Assuming that you are trying to access the file outside)

Upvotes: 3

kavita
kavita

Reputation: 425

  • I tried to parse the xml file inside other folders. It's printed successfully. Code is below

  • If you need an absolute path, you can also use Classloader to load the XML file.

    import java.io.File;
    import java.io.IOException;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
    
    public class Testing {
    
        public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
            parseXmlFile();
        }
    
        private static void parseXmlFile() throws ParserConfigurationException, SAXException, IOException {
            DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            domFactory.setIgnoringComments(true);
            DocumentBuilder builder = domFactory.newDocumentBuilder();
            Document doc = builder.parse(new File("src/main/java/xmlfiles/Test.xml"));
            if (doc.hasChildNodes()) {
                printNote(doc.getChildNodes());
    
            }
        }
    
        private static void printNote(NodeList nodeList) {
            for (int count = 0; count < nodeList.getLength(); count++) {
                Node tempNode = nodeList.item(count);
                if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
                    System.out.println("Value = " + tempNode.getTextContent());
                }
    
            }
        }
    }
    

<?xml version="1.0" encoding="UTF-8"?>
<company>
	<staff id="100">
		<firstname>Tom</firstname>
		<lastname>Jerry</lastname>
		<nickname>Tomy</nickname>
		<salary>100000</salary>
	</staff>
	<staff id="200">
		<firstname>Micky</firstname>
		<lastname>Mouse</lastname>
		<nickname>Mike</nickname>
		<salary>200000</salary>
	</staff>
</company>

Upvotes: 1

V. Morozov
V. Morozov

Reputation: 1

Try this:

    System.out.println(new File("").getAbsolutePath());

It will write your current working directory in console log. Then you can adjust your code like this:

    System.out.println(new File("relative/path/to/File.xml").exists);

It should tell you if the file(or directory) exists. Note that it's "/" and not "\".

Upvotes: 0

Benjamin Urquhart
Benjamin Urquhart

Reputation: 1649

Add .\\ to the beginning of your file name.

. stands for the current running directory

It looks like java is trying to find the folder from the root folder (C:\). Adding the . tells java to look inside the current running directory, not C:\. Even better, don't use backslashes and use a single forward slash:

new File("./MyFolder/Test.xml")

Upvotes: 0

Mohamed Anees A
Mohamed Anees A

Reputation: 4591

How about trying Document doc = builder.parse(new File("Use-cases\\testSuitesA_E_1002+${user}3_12022016+${date}2_2.5.xml"))

In your file path, Use-cases \testSuitesA_E_1002+${user}3_12022016+${date}2_2.5.xml \t represents an escape sequence.

Also, I'd love to check the {date} you are using, Maybe your date is formatted like 06\06\2018?

Upvotes: 1

Asha
Asha

Reputation: 67

I had the same problem recently and I found that in my case the file was saved as "abc.txt.txt" instead of "abc.txt".

As the extension of file was hidden I could not see earlier that the file was saved with an extension added to the name.

Check if the file is saved with proper extension.

Or, as your file name has "date" in it, it may be causing problem. Check if the date format while accessing the file is same as in the name of the file.

Upvotes: 0

Ravi
Ravi

Reputation: 190

Looks like you are using a relative path in the below mentioned line to access the file.

Document doc = builder.parse(new File("MyFolder\Test.xml"));

Also, you have used single '\', instead use '//'. You can debug using two options

  1. Try using absolute path to the file (always use '//') and see application has permission to access to the file. If access is there then form a correct relative path from the directory where the program is execurting.

  2. If for some reason your program is not able to access the having permission to access to the file then try provide the required permission.

Upvotes: 0

Related Questions