目录

String getPath()

描述 (Description)

java.io.File.getPath()方法将抽象路径名转换为路径名字符串。 要分隔名称序列中的名称,结果字符串将使用默认的名称 - 分隔符。

声明 (Declaration)

以下是java.io.File.getPath()方法的声明 -

public String getPath()

参数 (Parameters)

NA

返回值 (Return Value)

该方法返回此抽象路径名的路径名字符串形式。

异常 (Exception)

NA

例子 (Example)

以下示例显示了java.io.File.getPath()方法的用法。

package com.iowiki;
import java.io.File;
public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      String v;
      boolean bool = false;
      try {
         // create new file
         f = new File("test.txt");
         // pathname string from abstract pathname
         v = f.getPath();
         // true if the file path exists
         bool = f.exists();
         // if file exists
         if(bool) {
            // prints
            System.out.print("pathname: "+v);
         }
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      }
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

pathname: test.txt
↑回到顶部↑
WIKI教程 @2018