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

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值