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

歡迎訪問 生活随笔!

生活随笔

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

java

getparent_Java文件类字符串getParent()方法(带示例)

發布時間:2025/3/11 java 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 getparent_Java文件类字符串getParent()方法(带示例) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

getparent

文件類字符串getParent() (File Class String getParent())

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

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

  • This method is used to return the parent of the given file object(i.e In case of file object it returns the filepath where the file exists in the form of a string).

    此方法用于返回給定文件對象的父對象(即,如果是文件對象,則以字符串形式返回文件所在的文件路徑)。

  • The return type of this method is String (i.e It returns the parent of the file object and parent is in String form(i.e filepath is string where the file exists).

    此方法的返回類型為String(即,它返回文件對象的父級,并且父級為String形式(即,文件路徑是文件所在的字符串)。

Syntax:

句法:

String getParent(){}

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 i.e. It returns the parent of the given file object if parent does not exist then it returns null as a string.

此方法的返回類型為String,即,如果給定文件對象的父級不存在,則返回該父級,然后返回null作為字符串。

Java程序演示getParent()方法的示例 (Java program to demonstrate example of getParent() method)

// import the File class because we will use File class methods import java.io.*;// import the Exception class because it may raise an // exception when working with files import java.lang.Exception;public class GetParent {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("myjava.txt");// By using getParent() method returns a string which // contain the parent of the given file object.String file_parent1 = file1.getParent();// By using getParent() method returns null as a string // because there is no parent of the given file object.String file_parent2 = file2.getParent();// Display the parent of the given file objectSystem.out.println("The parent of the given file_parent1 is :" + file_parent1);System.out.println("The parent of the given file_paren2 is :" + file_parent2);} catch (Exception e) {System.out.println("An error occurred.");e.printStackTrace();}} }

Output

輸出量

D:\Programs>javac GetParent.javaD:\Programs>java GetParent The parent of the given file_parent1 is :C:\Users\computer clinic\OneDrive\Articles The parent of the given file_paren2 is :null

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

getparent

總結

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

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