本文共 3475 字,大约阅读时间需要 11 分钟。
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形式返回文件对象的完整路径。
// import the File class because we will use File class methodsimport java.io.File;// import the Exception class because it may raise an // exception when working with filesimport 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.javaD:\Programs>java GetAbsolutePathThe path of the file 1 if given path is absolute : C:\Users\computer clinic\OneDrive\myjava.txtThe path of the file2 if given path is not absolute : D:\Programs\java.txt
翻译自:
转载地址:http://hfazd.baihongyu.com/