简体   繁体   English

使用getClass()。getResource(“…”)。getFile()加载文件

[英]File loading using getClass().getResource(“…”).getFile()

first topic on stackoverflow, been using it for years, but can't find a solution to this: I need to fill a JTable with names of files in a directory (using Eclipse IDE). 关于stackoverflow的第一个主题已经使用了很多年,但是对此却找不到解决方案:我需要用目录中文件的名称填充JTable(使用Eclipse IDE)。

im using this: 即时通讯使用此:

 public void getFilesName()
    {
        File dir = new File(getClass().getResource("C:\\Files\\Server\\").getFile());

        File[] files = dir.listFiles();

        modelo.setColumnIdentifiers(new String[]{"Files Names"});

        Object[] row = new Object[1];

        for(int i = 0; i < files.length; i++)
        {
            row[0] = files[i].getName();

            modelo.addRow(row);
        }
    }

but it returns: 但它返回:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

and I can't find a way to do it. 而且我找不到办法。

Try to use: 尝试使用:

File dir = new File("C:\\Files\\Server\\");
File[] files = dir.listFiles();

Instead of: 代替:

File dir = new File(getClass().getResource("C:\\Files\\Server\\").getFile());
File[] files = dir.listFiles();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过 getClass().getResource() 加载文件 - File loading by getClass().getResource() 使用 getClass().getResource() 加载资源 - Loading resources using getClass().getResource() getClass().getResource() 加载文件失败,File() 有效 - File loading by getClass().getResource() fails, File() works getClass()。getResource在加载fxml文件中的作用是什么? - What is the role of getClass().getResource in loading fxml file? 使用getClass()。getResource将.txt文件检索到JTextArea - Using getClass().getResource to retrieve a .txt file to a JTextArea 当使用IntelliJ / Jetty 8.1.14运行项目时,this.getClass()。getResource(“”)。getFile()返回的路径开头为“ file:”。 - this.getClass().getResource(“”).getFile() returning path with “file:” at the beginning when running project with IntelliJ/Jetty 8.1.14 无法使用classLoader.getResource(fileName).getFile()获取文件 - unable to get file using classLoader.getResource(fileName).getFile() Java-使用getClass()。getResource()的文件路径不起作用 - Java - File path using getClass().getResource() does not work 使用 Main.class.getResource("notification.wav").getFile() 时找不到文件或语法不正确 - file not found or syntax incorrect when using Main.class.getResource("notification.wav").getFile() Java getClass().getResource(&quot;file&quot;) 导致 NullPointerException - Java getClass().getResource("file") leads to NullPointerException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM