getResourceAsStream返回null,尽管被调用的文件与getResourceAsStream类在同一目录中被调用

Kur*_*ner 2 java eclipse android amazon-web-services android-studio

我导入了一个由亚马逊编码的Android样本,涉及我从这里获得的AWS的DynamoDB,可能是为Eclipse编写的:https: //github.com/awslabs/aws-sdk-android-samples/tree/master/DynamoDBMapper_UserPreference

由于Android Studio(0.8.1)使用gradle而不是ant,因此在导入时自然会在dir结构方面自动移动,所以(部分)它看起来像这样:

在此输入图像描述

PropertyLoader获取从AwsCredentials.properties连接到数据库DynamoDB所需的TVM凭据信息.相关方法:

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." );
        }
    }
Run Code Online (Sandbox Code Playgroud)

但是getResourceAsStream行properties.load( this.getClass().getResourceAsStream( "AwsCredentials.properties" ) );返回null.正如您在我的屏幕截图中看到的,AwsCredentials.properties与PropertyLoader位于同一个目录中,并且与案例相匹配,根据我对该方法的读数,这应该是必需的:http: //mindprod.com/jgloss/getresourceasstream. HTML

getResourceAsStream()始终返回null

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

getResourceAsStream返回null

但是,这也没有解决问题.尝试过这些选项并完成研究后,我很困惑为什么它会返回null.我怎样才能解决这个问题?

Kur*_*ner 8

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

properties.load( PropertyLoader.class.getClassLoader().getResourceAsStream( "AwsCredentials.properties" ) );
Run Code Online (Sandbox Code Playgroud)

代替

properties.load( this.getClass().getResourceAsStream("AwsCredentials.properties" ) );
Run Code Online (Sandbox Code Playgroud)

不像我想的那么优雅,但它有效.