java获取当前文件夹名称

加入我的路径是F:\tj_game\log\a.txt
我想获得log的名称,其他都不要,也就是当前文件的所在的当前文件夹的名称
哪位高手会啊

第1个回答  2014-01-23
如果知道该文件的路径filePath
可以用 String parentFilePath = filePath.subString(0,filePath.lastIndexOf("\\")) 获取当前文件夹所在的路径
然后 fileFolderName = parentFilePath.subString(parentFilePath.lastIndexOf("\\"),parentFilePath.length()); 获取文件夹的名称。
第2个回答  2014-01-23
String path ="F:\\tj_game\\log\\a.txt";
path=path.substring(0,path.lastIndexOf("\\"));
System.out.println("path"+path);
File file = new File(path);
String name = file.getName();
System.out.print(name);
第3个回答  2015-07-09
import java.io.File;

public class getFileName {
public static void main(String [] args)
{
String path ="C:\\Documents and Settings\\AllUsers\\Documents\\My Pictures";
File file = new File(path);
String name = file.getName();
System.out.print(name);
}
}
第4个回答  推荐于2016-11-22

使用

System.getProperty("user.dir")

即可

第5个回答  2014-01-23
以split(“\”),取返回的数组的倒数第二个!