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

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

问题描述

我导入了一个由 Amazon 编码的 Android 示例,其中包含我从这里获得的 AWS DynamoDB,大概是为 Eclipse 编写的:

getResourceAsStream() 总是返回 null

我尝试了其他的东西,例如前缀\"(即 properties.load( this.getClass().getResourceAsStream( "\AwsCredentials.properties" ) ); 并复制凭证文件和放置在 src 文件夹中(您无法在此屏幕截图中看到它,因为资源管理器按文件类型(?)排序并首先放置main",但它在那里)如下:

getResourceAsStream 返回 null

然而,这也没有解决问题.尝试了这些选项并进行了研究后,我对它为什么返回 null 感到困惑.我该如何解决这个问题?

解决方案

在/src/main/下创建一个名为 resources 的目录并将 AwsCredentials.properties 放在那里并使用

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

代替

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 位于同一目录中,但 getResourceAsStream 仍返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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