jeudi 8 janvier 2015

Java fileNotFoundException when accessing program files


Vote count:

0




I cannot seem to by pass this error for the life of me. In my previous post i was trying to update attributes to an xml file. I can read the file just fine, but when I try and write to it I get a file not found exception.


The program doesn't have a problem with reading the XML file and finding the attribute only writing to it. After trouble shooting this for awhile, it seems to be an issue with having the file in the Program Files directory. If I move the xml file to C:\Temp\test.xml I can write to it without any issues. As soon as it goes into a folder with any type of spaces it cannot seem to find it. Seems like an issue with the StreamResults.



File file = new File(filePath);
document = documentBuilder.parse(file);
NodeList sessionNodelist = document.getElementsByTagName("session");

if (sessionNodelist.getLength() > 0)
{
Element sessionElement = (Element) sessionNodelist.item(0);
sessionElement.setAttribute("timeout", "12");
sessionElement.setAttribute("warning", "10");
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(document);
try{
StreamResult result = new StreamResult(file);
transformer.transform(source, result);
}catch(Exception e)
{
logger.info(e.getMessage());
}
}


java.io.FileNotFoundException: C:\Program%20Files\Test.xml (The system cannot find the path specified)


I am not exactly sure how to get around this error. You would think if it can read to it and find it in the first File call, the second file call should work right?


Any suggestions?



asked 24 secs ago







Java fileNotFoundException when accessing program files

Aucun commentaire:

Enregistrer un commentaire