java获取当前文件夹名字

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java获取当前文件夹名字相关的知识,希望对你有一定的参考价值。

就是我选择一个文件打开可以用System.out.print();输出文件夹的路径。
比如说是C:\Documents and Settings\All Users\Documents\My Pictures
我想只要当前My Pictures文件夹的名字。有没有大虾可以解决的。。。。。。
我只想获取My Pictures这个文件夹名,前面的路径不要。

定义对象File,传入路径参数,File提供方法getName直接获得路径的名称,即你想要的部分
File file = new File("xxxx");
System.out.println(file.getName());

-----------------------------------------------
String getName()
返回由此抽象路径名表示的文件或目录的名称。

参见参考资料

参考资料:http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/index.html

参考技术A String path ="C:\Documents and Settings\
AllUsers\Documents\MyPictures";
File file = new File(path);

String name = file.getNmae();
参考技术B File file3 = new File("e:\\Temple");
String folder = file3.getName();
参考技术C 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);



输出

My Pictures本回答被提问者采纳
参考技术D 孺子不可教,说的这么明白了还不懂么

Windows上获取当前文件夹中某个后缀名的所有文件的名字

Windows上获取当前文件夹中某个后缀名的所有文件的名字

----------

1.在需要获取文件名的文件夹中,新建一个txt文本,写入下面命令

dir /a-d /b *.txt>src.txt
echo 1111
pause

 

保存后修改后缀名为.bat

 

 

-----------------------------

以上是关于java获取当前文件夹名字的主要内容,如果未能解决你的问题,请参考以下文章

java web项目中,如何通过filter获取当前action的名字?请给出具体的代码

Windows上获取当前文件夹中某个后缀名的所有文件的名字

java:ServerSocket中获取所有连接上的Socket

java如何判图片的名字在这个路径下是不是存在

Runnable 怎么获取线程的名字

获取一个临时文件和对中文文件名字进行编码的工具类