getResourceAsStream返回null,尽管调用的文件与调用类getResourceAsStream在同一个目录中 [英] getResourceAsStream returning null despite called file being in same dir as class getResourceAsStream is called in

查看:131
本文介绍了getResourceAsStream返回null,尽管调用的文件与调用类getResourceAsStream在同一个目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我导入了一个由Amazon编写的Android示例代码,涉及到AWS的DynamoDB,我从这里得到的,大概是为Eclipse编写的:



getResourceAsStream()始终返回null



尝试其他的东西,如前缀\(即 properties.load(this.getClass()。getResourceAsStream(\AwsCredentials.properties)); 并复制凭证文件和放置在src文件夹(你不能看到它在这个屏幕截图中,因为浏览器按文件类型(?)排序,并且首先放置'main',但它在那里)按照以下原则: p>

getResourceAsStream返回null



但是,这还没有解决问题。尝试了这些选项并进行了研究,我很困惑为什么它返回null。如何解决这个问题?

解决方案

在/ src / main /下创建一个名为资源的目录,并在其中放置了AwsCredentials.properties

  properties.load(PropertyLoader.class.getClassLoader()。getResourceAsStream(AwsCredentials.properties)); 

而不是

 code> properties.load(this.getClass()。getResourceAsStream(AwsCredentials.properties)); 

不像我想要的那样优雅,但它的作品。


I imported an Android sample coded by Amazon involving AWS's DynamoDB which I got from here and was presumably written for Eclipse: https://github.com/awslabs/aws-sdk-android-samples/tree/master/DynamoDBMapper_UserPreference

Since Android Studio (0.8.1) uses gradle instead of ant, naturally things got auto-moved around in terms of dir structure when importing so (part of) it looks like this:

PropertyLoader gets the TVM credential info it needs to connect to the database DynamoDB from AwsCredentials.properties. Relevant methods:

public class PropertyLoader {

    private boolean hasCredentials = false;
    private String tokenVendingMachineURL = null;
    private boolean useSSL = false;
    private String testTableName = null;

    private static PropertyLoader instance = null;

    public static PropertyLoader getInstance() {
        if ( instance == null ) {
            instance = new PropertyLoader();
        }

        return instance;
    }

    public PropertyLoader() {
        try {
            Properties properties = new Properties();
            properties.load( this.getClass().getResourceAsStream( "AwsCredentials.properties" ) );

            this.tokenVendingMachineURL = properties.getProperty( "tokenVendingMachineURL" );
            this.useSSL = Boolean.parseBoolean( properties.getProperty( "useSSL" ) );
            this.testTableName = properties.getProperty( "testTableName" );

            if ( this.tokenVendingMachineURL == null || this.tokenVendingMachineURL.equals( "" ) || this.tokenVendingMachineURL.equals( "CHANGEME" ) || this.testTableName.equals( "" ) ) {
                this.tokenVendingMachineURL = null;
                this.useSSL = false;
                this.hasCredentials = false;
                this.testTableName = null;
            }
            else {
                this.hasCredentials = true;
            }
        }
        catch ( Exception exception ) {
            Log.e( "PropertyLoader", "Unable to read property file." );
        }
    }

However the getResourceAsStream line properties.load( this.getClass().getResourceAsStream( "AwsCredentials.properties" ) ); returns null. As you can see in my screenshot, AwsCredentials.properties is in the same dir as PropertyLoader and matches the case, which is all that should be required based on my readings of the method: http://mindprod.com/jgloss/getresourceasstream.html

getResourceAsStream() is always returning null

I have tried other things such as prefixing "\" (i.e. properties.load( this.getClass().getResourceAsStream( "\AwsCredentials.properties" ) ); and copying the credentials file and placing in the src folder (you can't see it in this screenshot because the explorer sorts by filetype(?) and places 'main' first, but it's there) as per this:

getResourceAsStream returning null

However, that hasn't fixed the issue either. Having tried these options and done research, I'm confused as to why it's returning null. How can I fix this?

解决方案

Created a dir called resources under /src/main/ and placed AwsCredentials.properties there and used

properties.load( PropertyLoader.class.getClassLoader().getResourceAsStream( "AwsCredentials.properties" ) );

instead of

properties.load( this.getClass().getResourceAsStream("AwsCredentials.properties" ) );

Not as elegant as I would like, but it works.

这篇关于getResourceAsStream返回null,尽管调用的文件与调用类getResourceAsStream在同一个目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆