java getname file_Java File getName()方法
Java File getName()方法
java.io.File.getName() 方法返回的路径名的名称序列的最后一个名字,这意味着表示此抽象路径名的文件或目录的名称被返回。
1 语法
public String getName()
2 参数
无
3 返回值
此方法返回的文件名或目录或空字符串,如果在路径名的名称序列为空。
4 示例
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.io.File.getName()方法的例子
*/
import java.io.File;
public class Demo {
public static void main(String[] args) {
File f = null;
File f1 = null;
String v, v1;
boolean bool = false;
try {
// create new files
f = new File("d:\test.txt");
f1 = new File("C:\Program Files");
// get file name or directory name
v = f.getName();
v1 = f1.getName();
// true if the file path exists
bool = f.exists();
// if file exists
if(bool) {
// prints
System.out.println("File name: "+v);
}
// true if the directory exists
bool = f1.exists();
// if the folder exists
if(bool) {
// prints
System.out.print("Folder name: "+v1);
}
} catch(Exception e) {
// if any error occurs
e.printStackTrace();
}
}
}
输出结果为:
File name: test.txt
Folder name: Program Files
Java File getName()方法 java.io.File.getName() 方法返回的路径名的名称序列的最后一个名字,这意味着表示此抽象路径名的文件或目录的名称被返回。 1 语法 public String getName() 2 参数 无 3 返回值 此方法返回的文件名或目录或空字符串,如果在路径名的名称序列为空。 4 示例 package com.yiidian; /** * 一点教程网: http://www.yiidian.com */ /** * java.io.File.getName()方法的例子 */ import java.io.File; public class Demo { public static void main(String[] args) { File f = null; File f1 = null; String v, v1; boolean bool = false; try { // create new files f = new File("d:\test.txt"); f1 = new File("C:\Program Files"); // get file name or directory name v = f.getName(); v1 = f1.getName(); // true if the file path exists bool = f.exists(); // if file exists if(bool) { // prints System.out.println("File name: "+v); } // true if the directory exists bool = f1.exists(); // if the folder exists if(bool) { // prints System.out.print("Folder name: "+v1); } } catch(Exception e) { // if any error occurs e.printStackTrace(); } } } 输出结果为: File name: test.txt Folder name: Program Files