文件类URI toURI()软件包java.io.File.toURI()中提供了此方法。
URI代表统一资源标识符。
此方法创建文件URI(即URI什么都不是,但它是文件路径)。
如果我们访问未授权的属性,则此方法将引发安全异常。
该方法可通过文件对象访问。
语法:URI toURI(){
}
参数:
我们不会在File方法中将任何对象作为参数传递。
返回值:
此方法的返回类型是URI,如果存在,它将返回文件的绝对文件路径。
Java程序演示toURI()方法示例//导入File类,因为我们将使用File类方法
import java.io.File;
//导入Exception类,因为它可能引发异常
//使用文件时
import java.lang.Exception;
//用于访问网络文件
import java.net.URI;
public class URIClass {
public static void main(String[] args) {
try {
//指定文件的路径,我们使用双斜杠
//为Windows转义'\'字符序列
URI uni_res_iden;
File file = new File("E:\\Programs\\myjava.txt");
if (file.exists()) {
uni_res_iden = file.toURI();
System.out.println("The URI of the file is : " + " " + uni_res_iden);
}
} catch (Exception e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
输出结果E:\Programs>javac URIClass.java
E:\Programs>java URIClass
The URI of the file is : file:/E:/Programs/myjava.txt