java.net.URISyntaxException

aay*_*oni 22 java xml exception

我得到了这个例外.但是这个例外不再重现.我想得到这个原因

Exception Caught while Checking tag in XMLjava.net.URISyntaxException:
Illegal character in opaque part at index 2:
C:\Documents and Settings\All Users\.SF\config\sd.xml
stacktrace net.sf.saxon.trans.XPathException.
Run Code Online (Sandbox Code Playgroud)

为什么发生这种异常.如何处理,所以它不会重现.

Ste*_*n C 42

基本上"C:\Documents and Settings\All Users\.SF\config\sd.xml"是路径名,而不是有效的URI.如果要将路径名转换为"file:"URI,请执行以下操作:

File f = new File("C:\Documents and Settings\All Users\.SF\config\sd.xml");
URI u = f.toURI();
Run Code Online (Sandbox Code Playgroud)

这是将路径名转换为Java中的有效URI的最简单,最可靠和最便携的方法.

但是你需要意识到"file:"URI有许多警告,如File.toURI()方法的javadocs所述.例如,在一台机器上创建的"file:"URI通常表示另一台机器上的不同资源(或根本没有资源).


小智 11

其根本原因是文件路径包含正斜杠而不是Windows中的反斜杠.

尝试这样来解决问题:

"file:" + string.replace("\\", "/");  
Run Code Online (Sandbox Code Playgroud)


小智 5

您必须具有以下字符串:

String windowsPath = file:/C:/Users/sizu/myFile.txt;
URI uri = new URI(windowsPath);
File file = new File(uri);
Run Code Online (Sandbox Code Playgroud)

通常,人们会执行以下操作:

String windowsPath = file:C:/Users/sizu/myFile.txt;
URI uri = new URI(windowsPath);
File file = new File(uri);
Run Code Online (Sandbox Code Playgroud)

或类似这样的东西:

String windowsPath = file:C:\Users\sizu\myFile.txt;
URI uri = new URI(windowsPath);
File file = new File(uri);
Run Code Online (Sandbox Code Playgroud)