The system cannot find the file specified but file exists The Next CEO of Stack OverflowHow do I check whether a file exists without exceptions?How do I copy a file in Python?How do I create a Java string from the contents of a file?Undo working copy modifications of one file in Git?How do I include a JavaScript file in another JavaScript file?Writing files in Node.jsHow to read a file line-by-line into a list?How do you append to a file in Python?Error while reading XML's child node - 3rd level of node - JavaTrying to read xml file using DOM Parser from jar file
Can we say or write : "No, it'sn't"?
How to count occurrences of text in a file?
Does Germany produce more waste than the US?
Solving system of ODEs with extra parameter
INSERT to a table from a database to other (same SQL Server) using Dynamic SQL
If Nick Fury and Coulson already knew about aliens (Kree and Skrull) why did they wait until Thor's appearance to start making weapons?
Newlines in BSD sed vs gsed
What flight has the highest ratio of time difference to flight time?
Does soap repel water?
Can a Bladesinger Wizard use Bladesong with a Hand Crossbow?
Do I need to write [sic] when a number is less than 10 but isn't written out?
WOW air has ceased operation, can I get my tickets refunded?
Math-accent symbol over parentheses enclosing accented symbol (amsmath)
Bartok - Syncopation (1): Meaning of notes in between Grand Staff
Chain wire methods together in Lightning Web Components
Is French Guiana a (hard) EU border?
What is the value of α and β in a triangle?
Why don't programming languages automatically manage the synchronous/asynchronous problem?
Which one is the true statement?
Proper way to express "He disappeared them"
Find non-case sensitive string in a mixed list of elements?
"misplaced omit" error when >centering columns
How many extra stops do monopods offer for tele photographs?
Method for adding error messages to a dictionary given a key
The system cannot find the file specified but file exists
The Next CEO of Stack OverflowHow do I check whether a file exists without exceptions?How do I copy a file in Python?How do I create a Java string from the contents of a file?Undo working copy modifications of one file in Git?How do I include a JavaScript file in another JavaScript file?Writing files in Node.jsHow to read a file line-by-line into a list?How do you append to a file in Python?Error while reading XML's child node - 3rd level of node - JavaTrying to read xml file using DOM Parser from jar file
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("MyFolderTest.xml"));
I am getting this error:
java.io.FileNotFoundException: C:MyFolderTest.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-casestestSuitesA_E_1002+$user3_12022016+$date2_2.5.xml".
java file
|
show 3 more comments
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("MyFolderTest.xml"));
I am getting this error:
java.io.FileNotFoundException: C:MyFolderTest.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-casestestSuitesA_E_1002+$user3_12022016+$date2_2.5.xml".
java file
It's in the wrong folder.
– jbx
Feb 28 at 16:09
3
"MyFolderTest.xml"
is not a valid String sinceT
is not a valid escape sequence
– Karol Dowbecki
Feb 28 at 16:10
No, that is not the problem. The real file I'm looking for is not called Test.xml
– Tal Angel
Feb 28 at 16:12
4
I suggest using forward slash/
. Otherwise you will often get issues going between windows paths and linux paths anyways. if you must use '', you would doFile("MyFolder\Test.xml")
– Zannith
Feb 28 at 16:12
6
@TalAngelt
is an escape in Java; it is interpreted as a tab character. You should either escape your backslashes (using\
) or replace them with/
.
– MC Emperor
Feb 28 at 16:24
|
show 3 more comments
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("MyFolderTest.xml"));
I am getting this error:
java.io.FileNotFoundException: C:MyFolderTest.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-casestestSuitesA_E_1002+$user3_12022016+$date2_2.5.xml".
java file
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("MyFolderTest.xml"));
I am getting this error:
java.io.FileNotFoundException: C:MyFolderTest.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-casestestSuitesA_E_1002+$user3_12022016+$date2_2.5.xml".
java file
java file
edited Mar 2 at 21:28
Tal Angel
asked Feb 28 at 16:00
Tal AngelTal Angel
36419
36419
It's in the wrong folder.
– jbx
Feb 28 at 16:09
3
"MyFolderTest.xml"
is not a valid String sinceT
is not a valid escape sequence
– Karol Dowbecki
Feb 28 at 16:10
No, that is not the problem. The real file I'm looking for is not called Test.xml
– Tal Angel
Feb 28 at 16:12
4
I suggest using forward slash/
. Otherwise you will often get issues going between windows paths and linux paths anyways. if you must use '', you would doFile("MyFolder\Test.xml")
– Zannith
Feb 28 at 16:12
6
@TalAngelt
is an escape in Java; it is interpreted as a tab character. You should either escape your backslashes (using\
) or replace them with/
.
– MC Emperor
Feb 28 at 16:24
|
show 3 more comments
It's in the wrong folder.
– jbx
Feb 28 at 16:09
3
"MyFolderTest.xml"
is not a valid String sinceT
is not a valid escape sequence
– Karol Dowbecki
Feb 28 at 16:10
No, that is not the problem. The real file I'm looking for is not called Test.xml
– Tal Angel
Feb 28 at 16:12
4
I suggest using forward slash/
. Otherwise you will often get issues going between windows paths and linux paths anyways. if you must use '', you would doFile("MyFolder\Test.xml")
– Zannith
Feb 28 at 16:12
6
@TalAngelt
is an escape in Java; it is interpreted as a tab character. You should either escape your backslashes (using\
) or replace them with/
.
– MC Emperor
Feb 28 at 16:24
It's in the wrong folder.
– jbx
Feb 28 at 16:09
It's in the wrong folder.
– jbx
Feb 28 at 16:09
3
3
"MyFolderTest.xml"
is not a valid String since T
is not a valid escape sequence– Karol Dowbecki
Feb 28 at 16:10
"MyFolderTest.xml"
is not a valid String since T
is not a valid escape sequence– Karol Dowbecki
Feb 28 at 16:10
No, that is not the problem. The real file I'm looking for is not called Test.xml
– Tal Angel
Feb 28 at 16:12
No, that is not the problem. The real file I'm looking for is not called Test.xml
– Tal Angel
Feb 28 at 16:12
4
4
I suggest using forward slash
/
. Otherwise you will often get issues going between windows paths and linux paths anyways. if you must use '', you would do File("MyFolder\Test.xml")
– Zannith
Feb 28 at 16:12
I suggest using forward slash
/
. Otherwise you will often get issues going between windows paths and linux paths anyways. if you must use '', you would do File("MyFolder\Test.xml")
– Zannith
Feb 28 at 16:12
6
6
@TalAngel
t
is an escape in Java; it is interpreted as a tab character. You should either escape your backslashes (using \
) or replace them with /
.– MC Emperor
Feb 28 at 16:24
@TalAngel
t
is an escape in Java; it is interpreted as a tab character. You should either escape your backslashes (using \
) or replace them with /
.– MC Emperor
Feb 28 at 16:24
|
show 3 more comments
7 Answers
7
active
oldest
votes
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("MyFolderTest.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)
add a comment |
How about trying Document doc = builder.parse(new File("Use-cases\testSuitesA_E_1002+$user3_12022016+$date2_2.5.xml"))
In your file path,
Use-cases testSuitesA_E_1002+$user3_12022016+$date2_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 0662018?
add a comment |
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>
add a comment |
Looks like you are using a relative path in the below mentioned line to access the file.
Document doc = builder.parse(new File("MyFolderTest.xml"));
Also, you have used single '', instead use '//'. You can debug using two options
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.
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.
add a comment |
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.
add a comment |
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")
add a comment |
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 "".
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54929686%2fthe-system-cannot-find-the-file-specified-but-file-exists%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
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("MyFolderTest.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)
add a comment |
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("MyFolderTest.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)
add a comment |
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("MyFolderTest.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)
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("MyFolderTest.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)
edited Mar 7 at 9:59
answered Mar 7 at 6:58
Anish B.Anish B.
392113
392113
add a comment |
add a comment |
How about trying Document doc = builder.parse(new File("Use-cases\testSuitesA_E_1002+$user3_12022016+$date2_2.5.xml"))
In your file path,
Use-cases testSuitesA_E_1002+$user3_12022016+$date2_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 0662018?
add a comment |
How about trying Document doc = builder.parse(new File("Use-cases\testSuitesA_E_1002+$user3_12022016+$date2_2.5.xml"))
In your file path,
Use-cases testSuitesA_E_1002+$user3_12022016+$date2_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 0662018?
add a comment |
How about trying Document doc = builder.parse(new File("Use-cases\testSuitesA_E_1002+$user3_12022016+$date2_2.5.xml"))
In your file path,
Use-cases testSuitesA_E_1002+$user3_12022016+$date2_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 0662018?
How about trying Document doc = builder.parse(new File("Use-cases\testSuitesA_E_1002+$user3_12022016+$date2_2.5.xml"))
In your file path,
Use-cases testSuitesA_E_1002+$user3_12022016+$date2_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 0662018?
answered Mar 4 at 12:18
Mohamed Anees AMohamed Anees A
1,226420
1,226420
add a comment |
add a comment |
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>
add a comment |
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>
add a comment |
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>
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>
<?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>
<?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>
answered Mar 7 at 16:39
kavitakavita
1737
1737
add a comment |
add a comment |
Looks like you are using a relative path in the below mentioned line to access the file.
Document doc = builder.parse(new File("MyFolderTest.xml"));
Also, you have used single '', instead use '//'. You can debug using two options
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.
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.
add a comment |
Looks like you are using a relative path in the below mentioned line to access the file.
Document doc = builder.parse(new File("MyFolderTest.xml"));
Also, you have used single '', instead use '//'. You can debug using two options
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.
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.
add a comment |
Looks like you are using a relative path in the below mentioned line to access the file.
Document doc = builder.parse(new File("MyFolderTest.xml"));
Also, you have used single '', instead use '//'. You can debug using two options
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.
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.
Looks like you are using a relative path in the below mentioned line to access the file.
Document doc = builder.parse(new File("MyFolderTest.xml"));
Also, you have used single '', instead use '//'. You can debug using two options
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.
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.
answered Mar 3 at 12:55
RaviRavi
652
652
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Mar 4 at 12:13
answered Mar 4 at 11:35
AshaAsha
134
134
add a comment |
add a comment |
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")
add a comment |
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")
add a comment |
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")
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")
answered Mar 5 at 16:03
Benjamin UrquhartBenjamin Urquhart
3847
3847
add a comment |
add a comment |
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 "".
add a comment |
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 "".
add a comment |
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 "".
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 "".
answered Mar 6 at 16:23
V. MorozovV. Morozov
11
11
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54929686%2fthe-system-cannot-find-the-file-specified-but-file-exists%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It's in the wrong folder.
– jbx
Feb 28 at 16:09
3
"MyFolderTest.xml"
is not a valid String sinceT
is not a valid escape sequence– Karol Dowbecki
Feb 28 at 16:10
No, that is not the problem. The real file I'm looking for is not called Test.xml
– Tal Angel
Feb 28 at 16:12
4
I suggest using forward slash
/
. Otherwise you will often get issues going between windows paths and linux paths anyways. if you must use '', you would doFile("MyFolder\Test.xml")
– Zannith
Feb 28 at 16:12
6
@TalAngel
t
is an escape in Java; it is interpreted as a tab character. You should either escape your backslashes (using\
) or replace them with/
.– MC Emperor
Feb 28 at 16:24