Java文件类字符串getAbsolutePath()方法(带示例)

文件类字符串getAbsolutePath() (File Class String getAbsolutePath())

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

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

  • This method is used to return the absolute path of the file object (i.e absolute path is the complete path like this c:\\ Articles\\myjava.txt) if filepath is absolute then it retrieve the complete path of the file object.

    如果filepath是绝对路径,则此方法用于返回文件对象的绝对路径(即,绝对路径是诸如c:\\ Articles \\ myjava.txt这样的完整路径),然后它将检索文件对象的完整路径。

  • The return type of this method is String so it returns the absolute path from the root in a string form.

    此方法的返回类型为String,因此它以字符串形式返回从根开始的绝对路径。

  • In this method, if we don’t give an absolute path in the file object then also it will return the absolute path of file object where your file exists.

    在这种方法中,如果我们没有在文件对象中给出绝对路径,那么它将返回文件所在的文件对象的绝对路径。

  • This method may raise an exception( i.e. Security Exception) if the desired value cannot be accessed.

    如果无法访问所需的值,则此方法可能引发异常(即Security Exception)。

Syntax:

句法:

    String getAbsolutePath(){
    }

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 String so it returns the complete path of the file object as a String.

该方法的返回类型为String,因此它以String形式返回文件对象的完整路径。

Java程序演示getAbsolutePath()方法的示例 (Java program to demonstrate example of getAbsolutePath() 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;

public class GetAbsolutePath {
    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.
            File file1 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt");
            File file2 = new File("java.txt");

            // By using getAbsolutePath() return the complete 
            // path(whatever you have given in the file object) of the 
            // file object because in the file1 object we already given 
            // absolute path 
            // [C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt]
            String abs_path1 = file1.getAbsolutePath();

            // Display absolute path of the file object if given path is absolute.
            System.out.println("The path of the file 1 if given path is absolute :" + " " + abs_path1);

            // By using getAbsolutePath() return the complete path of the File 
            // object even we have not given full path or absolute path is 
            // not given [i.e.(java.txt) so it return the whole path 
            // with filename where file exists ]

            String abs_path2 = file2.getAbsolutePath();

            // Display absolute path of the file object if given path is not absolute.
            System.out.println("The path of the file2 if given path is not absolute :" + " " + abs_path2);

        } catch (Exception e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Output

输出量

D:\Programs>javac GetAbsolutePath.java

D:\Programs>java GetAbsolutePath
The path of the file 1 if given path is absolute : C:\Users\computer clinic\OneDrive\myjava.txt
The path of the file2 if given path is not absolute : D:\Programs\java.txt


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值