1

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?

UPDATE: I tried some other methods.

So when i have the file path set to "C:\Program Files\test.xml" File.exists returns ture, along with read and writing. if I add the %20 to the program file path, they all return false" E.g C:\Program%20Files\test.xml.

So document = documentBuilder.parse(file); can parse the file just perfectly fine.

When StreamResults trys to open the file, it thoes the file not found error and displays the %20 in the Program files name.

StreamResult result = new StreamResult(file);
transformer.transform(source, result);

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

Is there another way to stream the results to the xml file instead of StreamResults?

Code Kadiya
  • 385
  • 1
  • 14
user1158745
  • 2,402
  • 9
  • 41
  • 60

3 Answers3

1

I figured it out. After doing a ton of reading about other people having a simular problem i had to do the following work around in order for it to work correctly.

  StreamResult result = new StreamResult(file.getPath());
  transformer.transform(source, result);

It now works. Odd, but it works.

user1158745
  • 2,402
  • 9
  • 41
  • 60
0

Instead of Using :

String filePath = "C:\Program%20Files\Test.xml";

Use this :

String filePath = "C:\\Program%20Files\\Test.xml";

The problem lies with parsing the "\" charatecter

EDIT : I do not have that much of experirnce with Java's File I/O But Here is what i found :

File file = new File(filePath);
System.out.println(file.canRead());  // false
System.out.println(file.canWrite());  // false

This might be the reason behind the problem here (Excpert 's wisdom neede here for clarification).

Amitesh Rai
  • 866
  • 11
  • 21
  • Same issue. I hard coded it as "C:\\Program%20File\\Test.xml At the top my code, the DocumentBuilder has no issues find the file and opening it. But the StreamResults does. It makes me think that StreamResults has a bug. Is there another Alt to useing the StreamResults to write back to the xml file? – user1158745 Jan 08 '15 at 21:18
  • @user1158745 file.canWrite() is returning false, this might have to do something with the problem you are facing – Amitesh Rai Jan 08 '15 at 21:28
  • This is baffling, i tested file.canRead and write and both return false, however, i can read the file. When I set the file to C:\\Temp\test.xml both return true. What would you suggest to trouble shoot the file canREad and write/read issue? – user1158745 Jan 08 '15 at 21:42
  • Well I also tried, file.exsits and it returns false. So I think the read and write are correct since it cannot find the file. I still think it's an issue with accessing the file. – user1158745 Jan 08 '15 at 21:48
  • I take that back. So if I add the %20 to the file path, it cannot find the file. If i remove the %20, then does file exist returns true, read and write also returns true, but StreamResult result = new StreamResult(file); transformer.transform(source, result); is called, I still get the file not found exception and in the message it has %20 in Program Files e.g C:\Program%20Files – user1158745 Jan 08 '15 at 21:51
  • @user1158745 Intresting to find this behaviour. I also got the same observations with and without the %20 in the file pathe name. Without "%20" it return true and with %20 it return "false" – Amitesh Rai Jan 08 '15 at 22:02
0

It seems that File cannot locate the file, I think because of a problem in the path.

The path could be relative or absolute. You could try to make it relative, and check back in if it worked...

Tim Visser
  • 916
  • 9
  • 28