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.mzph.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、弱电项目…