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 object
System.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.java
D:\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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542124.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542124.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542124.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python—while循環、字符串

1. while循環: while 條件滿足:滿足條件執行的語句 else:不滿足條件執行的語句while 死循環 只要滿足 while的條件永遠為真,就會進入無限循環 while True:print(一直循環)while嵌套 \t :在控制臺輸出一個制表符,協助…

java基礎——java基本運算

java基本運算 轉載于:https://www.cnblogs.com/zhouj/p/6132535.html

【Java】MybatisPlus

MybatisPlus MybatisPlus是在mybatis基礎上的一個增強型工具。它對mybatis的一些操作進行了簡化,能夠提高開發的效率。 springboot整合了mybatis之后,其實已經非常方便了,只需要導入mybatis的包后,在配置文件中編寫數據源信息&a…

機器人總動員擬人后_WALL·E 機器人總動員——又是一部讓名字耽誤的高分電影...

WALLE 機器人總動員(2008)導演: 安德魯斯坦頓簡單,卻惹人落淚的動畫電影。名字叫做機器人總動員,說實話,這部電影高高的掛在豆瓣的排行榜上,但卻因為名字被我總是忽視掉,我好像就是這么膚淺的人呀~電影是由…

Java DataInputStream readBoolean()方法(帶示例)

DataInputStream類readBoolean()方法 (DataInputStream Class readBoolean() method) readBoolean() method is available in java.io package. readBoolean()方法在java.io包中可用。 readBoolean() method is used to check whether this stream read the boolean value or n…

python—列表

1 . 什么是列表: 列表形如[‘saf’,‘sf’,5,哈哈‘],是一個可以儲存任意數據類型的集合。需要注意的是,列表和數組不同,數組是儲存統一數據類型的集合。 創建一個列表: zero_li [ ] #建立一個空列表li [ ‘http’…

更新SQL Server實例所有數據庫表統計信息

引出問題 自從上次菜鳥為老鳥解決了《RDS SQL SERVER 解決中文亂碼問題》問題,老鳥意猶未盡,決定再想個招來刁難刁難菜鳥:“我最近做T-SQL性能調優的時候,經常發現執行計劃中的統計信息不準確,導致SQL Server查詢性能低…

職工考勤管理信息系統數據庫課設_數據庫課程設計--職工考勤管理信息系統

目錄1概述........................................................................................................................................ 11.1設計背景..................................................................................................…

python—元組

1 . 元組的創建: 1 . 簡介 :元組與列表類似,可以存儲任意數據類型。不同之處在于元組的元素不能修改。屬于不可變數據類型,沒有增刪改。 2 . 創建一個元組: t ([1, 2, 3], ssh, True) #和列表不同是,元組…

java clock計時_Java Clock類| systemDefaultZone()方法與示例

java clock計時Clock Class systemDefaultZone()方法 (Clock Class systemDefaultZone() method) systemDefaultZone() method is available in java.time package. systemDefaultZone()方法在java.time包中可用。 systemDefaultZone() method is used to get the current inst…

從0開始搭建SQL Server AlwaysOn 第四篇(配置異地機房節點)

從0開始搭建SQL Server AlwaysOn 第四篇(配置異地機房節點) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www.cnblogs.com/lyhabc/p/4682028.html第三篇http://www.cnblogs.com/lyhabc/p/4682986.html第四篇http://www.cnblogs.com…

vue 左側菜單隱藏_vue.js 左側二級菜單顯示與隱藏切換的實例代碼

vue點擊切換顯示隱藏*{padding: 0;margin: 0;font-size: 14px;}ul{width: 200px;height: auto;}h2{background: green;border: 1px solid #fff;color: #fff;height: 30px;line-height: 30px;text-indent: 24px;}h3{background: #999;height: 24px;line-height: 24px;border: 1p…

Java LocalDateTime類| 帶示例的getMinute()方法

LocalDateTime類getMinute()方法 (LocalDateTime Class getMinute() method) getMinute() method is available in java.time package. getMinute()方法在java.time包中可用。 getMinute() method is used to get minute-of-hour field value from this date-time object. getM…

python—集合

1. 集合的概念和建立: Python中的集合和數學中的集合類似,可以保存不重復的元素。它有可變集合(set)和不可變集合(frozenset)兩種。 集合是不重復的并且無序的序列。 集合創建可以使用{}以及set()的方式。…

第十九天

今天是學習js的第三天,已經感覺腦內存不夠用了。 想想后續還有這么多Php的課程要學習,真心有點怕怕的。希望以后時間過的慢一些,讓我好好的掌握這門技術吧。 另外,學習js里面還有好多的應用需要自己去掌握,我希望能夠通…

彈性理論法研究樁基受力計算公式_豎向荷載下群樁的承載力分析

豎向荷載下群樁的承載力分析樁基礎在工程建設當中得到廣泛地應用,從安全性上考慮,對群樁承載力的研究尤為重要。文章結合一工程實例,根據現場取得的靜載荷試驗數據,(本文共4頁)閱讀全文>>墻體頂部斜裂縫問題一直是困擾設計和施工人員的重要課題,以往的研究往往將這種斜裂…

Java LocalDate類| getChronology()方法與示例

LocalDate類getChronology()方法 (LocalDate Class getChronology() method) getChronology() method is available in java.time package. getChronology()方法在java.time包中可用。 getChronology() method is used to return the IsoChronology of this LocalDate object. …

python—字典

1.字典的含義: 字典和列表類似,是可變序列,與列表不同的是,字典是無序的可變序列,保存的內容是以 鍵值對 的形式存放的。鍵是唯一的,而值可以是多個。值可以取任意數據了類型,但鍵必須是不可變…

js中加“var”和不加“var”的區別,看完覺得這么多年js白學了

Javascript聲明變量的時候,雖然用var關鍵字聲明和不用關鍵字聲明,很多時候運行并沒有問題,但是這兩種方式還是有區別的。可以正常運行的代碼并不代表是合適的代碼。var num 1; 是在當前域中聲明變量. 如果在方法中聲明&#xff0…

解決方案_智能工廠全套解決方案

最近弱電社群資料更新情況:1、弱電學習圈VIP群資料整理-希望對您有用!2、弱電學習圈VIP技術交流2群成立,歡迎您加入!3、智慧校園整體解決方案!4、智能化弱電項目管理表單大全-弱電項目經理必備!5、弱電項目…