日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發布時間:2023/12/1 java 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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.javaD:\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

總結

以上是生活随笔為你收集整理的Java文件类字符串getAbsolutePath()方法(带示例)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。