Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to read a file which located in my document directory

but the compiler throws an exception. says that there is a problem in the path of the file I specified

this is a segment of my code

String path = filePath;
                    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
 DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
    System.out.println(path);
          document =  builder.parse(path);


and this is the exception thrown:
MalformedURLException: unknown protocol: c

I knew it says there is problem in the path format. but I print it and it looks RIGHT
this is the path of the file I want to read
C:\Documents and Settings\AlOne\My Documents\SRS projects\HR.xml


plz Help Im tired from searching ;O
Posted

1 solution

The first (of about 189000) link here[^] says

Thrown to indicate that a malformed URL has occurred. Either no legal protocol could be found in a specification string or the string could not be parsed.

Your message then says "unknown protocol". Did you put "file:///" in front of the file name?
I suggest you read up on the format of URLs.

If you're tired of searching, might I suggest that you are looking in the wrong places.

Peter
 
Share this answer
 
Comments
Hackaholic 3-May-11 7:58am    
I posted my question here so those who have experience answer me!
I googled it b4 I ask but I didnt find the sufficient answer.

thnx anyway Bro xD
Hackaholic 3-May-11 9:04am    
I found the solution
simply replace the path string with
document = builder.parse(new File(path).toURI().toString());

Cheers!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900