[英]How to show the file name only in JList?
以下代码显示整个目录路径:
JList list = new JList(new File(path).listFiles());
我只需要文件名,而不需要路径。 如何更改代码以执行此操作?
我尝试了这个:
String path = "C:\\Users\\" + username + "\\AppData\\Roaming\\images\\jpgs";
JList list = new JList((new File(path.toString()).listFiles()).getName());
但我得到一个错误。 有谁知道如何解决?
尝试这个
尝试{File [] fileList =(new File(“ D:\\ Software”))。listFiles();
String[] fileName = new String[fileList.length];
for(int i=0; i<fileList.length;i++)
{
fileName[i] = fileList[i].getName();
}
JList<String> jList = new JList<String>(fileName);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
用这个:
new File(absolutePath).getName();
如您所见( 重复的问题 ),您必须使用absolutePath
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.