java uri 文件_Java文件类URI toURI()方法(带示例)

java uri 文件

文件类URI toURI() (File Class URI toURI())

  • This method is available in package java.io.File.toURI().

    软件包java.io.File.toURI()中提供了此方法。

  • URI stands for Uniform Resource Identifier.

    URI代表统一资源标识符。

  • This method creates a file URI (i.e. URI is nothing but it is a filepath).

    此方法创建一个文件URI(即URI什么都不是,但它是一个文件路径)。

  • This method will throw a security exception if we access an unauthorized property.

    如果我们访问未授权的属性,则此方法将引发安全异常。

  • This method is accessible with file object.

    该方法可通过文件对象访问。

Syntax:

句法:

    URI toURI(){
    }

Parameter(s):

参数:

We don't pass any object as a parameter in the method of the File.

我们不会在File方法中将任何对象作为参数传递。

Return value:

返回值:

The return type of this method is URI, it returns absolute filepath of the file if exists.

此方法的返回类型为URI ,如果存在,则返回文件的绝对文件路径。

Java程序演示toURI()方法的示例 (Java program to demonstrate example of toURI() method)

// import the File class because we will use File class methods
import java.io.File;

// import the Exception class because it may raise an exception 
// when working with files
import java.lang.Exception;

// For accessing net files
import java.net.URI;

public class URIClass {
    public static void main(String[] args) {
        try {
            // Specify the path of file and we use double slashes to 
            // escape '\' character sequence for windows otherwise 
            // it will be considerable as url.
            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();
        }
    }
}

Output

输出量

E:\Programs>javac URIClass.java

E:\Programs>java URIClass
The URI of the file is :  file:/E:/Programs/myjava.txt


翻译自: https://www.includehelp.com/java/file-class-uri-touri-method-with-example.aspx

java uri 文件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值