/**
* Returns the name of the file or directory denoted by this abstract
* pathname. This is just the last name in the pathname's name
* sequence. If the pathname's name sequence is empty, then the empty
* string is returned.
*
* @return The name of the file or directory denoted by this abstract
* pathname, or the empty string if this pathname's name sequence
* is empty
*/
public String getName() {
int index = path.lastIndexOf(separatorChar);
if (index < prefixLength) return path.substring(prefixLength);
return path.substring(index + 1);
}
* Returns the name of the file or directory denoted by this abstract
* pathname. This is just the last name in the pathname's name
* sequence. If the pathname's name sequence is empty, then the empty
* string is returned.
*
* @return The name of the file or directory denoted by this abstract
* pathname, or the empty string if this pathname's name sequence
* is empty
*/
public String getName() {
int index = path.lastIndexOf(separatorChar);
if (index < prefixLength) return path.substring(prefixLength);
return path.substring(index + 1);
}
System.out.println(file.getName());//21212
File(String pathname)
Creates a new File instance by converting the given pathname string into an abstract pathname.
这样写似乎没有问题,我怀疑参数名和文件名是一样的文件这块还没有了解坐等大神
默认的就是当前Java文件路径啊,文件名是点。
如果要在其他地方创建:
//文件名
String name = "face.gif";
//源文件的路径
File f = new File("d:\\"+name);
如果路径"d:\\"中含.,那.表示当前路径,不过要注意写法。
f.getName 你这样输出 就是 1.txt!