getResource还没有工作getResourceAsStream

问题描述 投票:1回答:1

我有一个java程序,它是产品的扩展。我希望检查文件是否已更改,然后复制它(如果有)。

我正在使用ClassLoader来获取资源,以便我可以获得最后修改日期。 I:E

        boolean copyFile = false;
    String fileName = getRhumbaDirectory()+"\\stockexchanges.dict";
    try {
            File file = new File(fileName);
            Long fileLastModified = file.lastModified();
            URL url = this.getClass().getClassLoader().getResource("com/moneydance/modules/features/securityquoteload/resources/stockexchanges.dict");
            Long resourceLastModified=0L;
            if (url !=null) {
                resourceLastModified = url.openConnection().getLastModified();
            }
            debugInst.debug("ExchangeList", "getData", MRBDebug.INFO, "Modified Date "+fileLastModified+" "+ resourceLastModified);     
            if (resourceLastModified > fileLastModified)
                copyFile = true;
    }
    catch (IOException e){
        copyFile = true;
    }
    if (copyFile) {
        try {
            InputStream input = this.getClass().getClassLoader().getResourceAsStream("com/moneydance/modules/features/securityquoteload/resources/stockexchanges.dict");
            if (input == null) {
                debugInst.debug("ExchangeList", "getData", MRBDebug.INFO, "Problem creating stockexchanges.dict file");     
            }
            else {
                FileOutputStream output = new FileOutputStream(fileName);
                byte [] buffer = new byte[4096];
                int bytesRead = input.read(buffer);
                while (bytesRead != -1) {
                    output.write(buffer, 0, bytesRead);
                    bytesRead = input.read(buffer);
                }
                output.close();
                input.close();
            }
        }
        catch (IOException f) {
            debugInst.debug("ExchangeList", "getData", MRBDebug.DETAILED, "Problem copying default file"+f.getMessage());       
            f.printStackTrace();
        }

    }

代码

                URL url = this.getClass().getClassLoader().getResource("com/moneydance/modules/features/securityquoteload/resources/stockexchanges.dict");

代码返回null

                InputStream input = this.getClass().getClassLoader().getResourceAsStream("com/moneydance/modules/features/securityquoteload/resources/stockexchanges.dict");

返回有效的流。他们俩不应该工作吗?

java classloader
1个回答
0
投票

感谢'@Stephen C'他的建议找到了原因。该论坛无法提供应用专有的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.