簡體   English   中英

如何僅在JList中顯示文件名?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM