URI toURI()
描述 (Description)
java.io.File.toURI()方法创建一个文件:URI,表示抽象路径名。
声明 (Declaration)
以下是java.io.File.toURI()方法的声明 -
public URI toURI()
参数 (Parameters)
NA
返回值 (Return Value)
该方法返回一个绝对的分层URI,其方案等于“file”。
异常 (Exception)
SecurityException - 如果无法访问所需的系统属性值
例子 (Example)
以下示例显示了java.io.File.toURI()方法的用法。
package com.iowiki;
import java.io.File;
import java.net.URI;
public class FileDemo {
public static void main(String[] args) {
File f = null;
URI uri;
boolean bool = false;
try {
// create new File object
f = new File("c:/java test.txt");
// returns true if file exists
bool = f.exists();
// if file exists
if(bool) {
// returns the uri string
uri = f.toURI();
// print
System.out.println("uri: "+uri);
}
} catch(Exception e) {
// if any error occurs
e.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生以下结果 -
uri: file:/c:/java%20test.txt