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

问题描述

我进口亚马逊在Android样品codeD,涉及AWS的DynamoDB,我在这里得到了,是$ P $为Eclipse psumably写的:的

由于Android的工作室(0.8.1)使用摇篮,而不是蚂蚁,自然的东西得到了自动移动目录结构方面进口,所以当周围的(部分),它看起来是这样的:

PropertyLoader得到它需要连接到从AwsCredentials.properties数据库DynamoDB的TVM凭证信息。有关方法:

 公共类PropertyLoader {

    私人布尔hasCredentials = FALSE;
    私人字符串tokenVendingMachineURL = NULL;
    私人布尔useSSL = FALSE;
    私人字符串testTableName = NULL;

    私有静态PropertyLoader实例= NULL;

    公共静态PropertyLoader的getInstance(){
        如果(例如== NULL){
            例如=新PropertyLoader();
        }

        返回实例;
    }

    公共PropertyLoader(){
        尝试 {
            属性属性=新的属性();
            properties.load(this.getClass()的getResourceAsStream(AwsCredentials.properties));

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

            如果(this.tokenVendingMachineURL ==空|| this.tokenVendingMachineURL.equals()|| this.tokenVendingMachineURL.equals(CHANGEME)|| this.testTableName.equals()){
                this.tokenVendingMachineURL = NULL;
                this.useSSL = FALSE;
                this.hasCredentials = FALSE;
                this.testTableName = NULL;
            }
            其他 {
                this.hasCredentials = TRUE;
            }
        }
        赶上(例外的例外){
            Log.e(PropertyLoader,无法读取属性文件。);
        }
    }
 

然而,的getResourceAsStream行 properties.load(this.getClass()的getResourceAsStream(AwsCredentials.properties)); 返回null。正如你在我的截图看到,AwsCredentials.properties是在同一个目录作为PropertyLoader和相匹配的情况下,这是所有应需要根据我的方法阅读: http://mindprod.com/jgloss/getresourceasstr​​eam.html

getResourceAsStream()总是返回null

我已经尝试过其他的东西,如prefixing\(即 properties.load(this.getClass()的getResourceAsStream(\ AwsCredentials.properties)); 和复制凭证文件,并把src文件夹(你不能看到它在这个截图,因为浏览器排序按文件类型()和地方的主要'第一,但它的存在?)按这样的:

getResourceAsStream返回null

不过,这并没有固定的问题无论是。尝试过这些选项,做研究,我很困惑,为什么它返回null。我该如何解决这个问题?

解决方案

在创建/ src中一个目录叫做资源/主/和放置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返回null,尽管被调用文件中相同的目录是作为类的getResourceAsStream被称为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 21:08