Java Java.io.File.toURI() 方法

  • 描述

    java.io.File.toURI()方法创建一个文件:代表抽象路径名的URI。
  • 声明

    以下是java.io.File.toURI()方法的声明-
     public URI toURI()
  • 参数

    不适用
  • 返回值

    该方法返回一个绝对的,层次结构URI,其方案等于“文件”。
  • 异常

    SecurityException如果无法访问必需的系统属性值
  • 例子

    以下示例显示java.io.File.toURI()方法的用法。
     
    package com.jc2182;
    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