Java.io.File.toURI() 方法
描述
java.io.File.toURI() 方法创建一个 file: 表示抽象路径名的 URI。
声明
以下是 java.io.File.toURI() 方法的声明 −
public URI toURI()
参数
NA
返回值
该方法返回一个绝对的、分层的 URI,其方案等于"file"。
异常
SecurityException − 如果无法访问所需的系统属性值
示例
下面的例子展示了 java.io.File.toURI() 方法的使用。
package com.tutorialspoint;
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