java getname_Java文件类字符串getName()方法(带示例)

java getname

文件类字符串getName() (File Class String getName())

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

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

  • This method is used to retrieve or return the filename or directory name and represented by the file path.

    此方法用于检索或返回文件名或目录名,并由文件路径表示。

Syntax:

句法:

    String getName(){
    }

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 that means this method returns the name of file or directory and the name is in string type that's why return type of this method is a string.

该方法的返回类型为String ,这意味着该方法返回文件或目录的名称,并且该名称为字符串类型,这就是该方法的返回类型为字符串的原因。

Java程序演示getName()方法的示例 (Java program to demonstrate example of getName() 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 GetNameOfFile {
    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 file = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt");

            // By using getName() we can find the name of the file 
            // or directory and file should exists before 
            System.out.println("The Name Of The File created is : " + file.getName());

        } catch (Exception e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Output

输出量

D:\Programs>javac GetNameOfFile.java

D:\Programs>java GetNameOfFile
The Name Of The File created is : C:\Users\computer clinic\OneDrive\Articles\myjava.txt


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

java getname

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值