简体   繁体   English

当我使用getResourceAsStream读取Android Studio中的属性文件时,获取“ null”

[英]Get “null” when I'm using getResourceAsStream to read properties file in Android Studio

I recently just start to using Android Studio for my programming study. 我最近刚开始使用Android Studio进行编程研究。

I've faced a problem today which is keep getting "null" when I using getResourceAsStream to read a properties file from JUNIT TEST. 我今天遇到了一个问题,当我使用getResourceAsStream从JUNIT TEST读取属性文件时,它总是变得“空”。

I used to locate the .properties file under "src" directory when I was using Eclipse. 在使用Eclipse时,我曾经将.properties文件放在“ src”目录下。 But in Android Studio, it won't work. 但是在Android Studio中,它将无法正常工作。

Here is the code that part of a class called BeanFactory,: 这是称为BeanFactory的类的一部分的代码:

private static Properties properties;

static {
    properties = new Properties();
    InputStream is = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
    try {
        properties.load(is);
    }catch (IOException e) {
        e.printStackTrace();
    }
}

In Eclipse, I located the the prop file under src, and it works just fine. 在Eclipse中,我将prop文件放在src下,并且工作正常。

But in Android Studio, I've tried put the "bean.properties" file in several different directory. 但是在Android Studio中,我尝试将“ bean.properties”文件放在几个不同的目录中。 Such as /src, or/src/main/java, nothing worked out. 例如/ src或/ src / main / java,没有任何结果。

Where should I put the prop file? 我应该在哪里放置道具文件? Is there any configurations that I should do for this? 我应该为此做些配置吗?

Placing resources into assets is not acceptable in some cases. 在某些情况下,将资源放入资产中是不可接受的。 I solved this problem by packing required resourses into special jar file and placing this jar into libs directory.Thus you still can access them via standard Java technique. 我通过将所需资源打包到特殊的jar文件中并将此jar放入libs目录中解决了这个问题,因此您仍然可以通过标准Java技术访问它们。

There are two issues. 有两个问题。 In general resources are stored in a jar, with the compiled .class files. 通常,资源与已编译的.class文件一起存储在jar中。 getResource and getResourceAsStream are methods of a class. getResourcegetResourceAsStream是类的方法。 The resource is searched in the jar of this class (in general). 通常在此类的jar中搜索资源。 So with inheritance getClass().getResource("...") might be dangerous. 因此,通过继承getClass().getResource("...")可能很危险。

The second, more grave pitfall is, that the path is relative to the package (directory) of the class, unless you use "/...". 第二个更严重的陷阱是,除非您使用“ / ...”,否则该路径是对于类的包(目录)的。

Another way is to use ResourceBundle instead of Properties. 另一种方法是使用ResourceBundle而不是Properties。 ResourceBundle has a PropertiesResourceBundle for *.properties. ResourceBundle具有* .properties的PropertiesResourceBundle。

ResourceBundle properties = ResourceBundle.getBundle("bean");

try this: 尝试这个:
1.put your own bean.properties file in app/src/main/assets 1.将自己的bean.properties文件放入app / src / main / assets
2.change you code to: 2.将您的代码更改为:

private static Properties prop;

static {
    prop = new Properties();
    try {
        InputStream is = IWorkApplication.getInstance().getResources().getAssets().open("bean.properties", Context.MODE_PRIVATE);
        prop.load(is);
    } catch (IOException e) {
        e.printStackTrace();
    }

PS. PS。 IWorkApplication.getInstance() is define in my own application class. IWorkApplication.getInstance()在我自己的应用程序类中定义。
Hope to be useful 希望有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我无法在类路径中使用getResourceAsStream读取属性文件(在WEB-INF / lib下) - Why I cant read properties file using getResourceAsStream when it is in classpath (under WEB-INF/lib) 读取属性文件时 getResourceAsStream 返回 null - getResourceAsStream returns null when reading properties file getResourceAsStream() 返回 null。 未加载属性文件 - getResourceAsStream() is returning null. Properties file is not loading 尝试检索JSON文件时,getResourceAsStream与android返回null - getResourceAsStream returning null with android when trying to retrieve JSON file 使用getResourceAsStream使用他的完整路径读取Android文件 - Using getResourceAsStream to read an Android file using his full-path 无法使用 getResourceAsStream 读取文件 - unable to read file using getResourceAsStream getResourceAsStream()仅在jar文件中返回null - getResourceAsStream() returns null only when in jar file java.io.FileNotFoundException 当我试图读入我的 Android Studio 项目中的二进制文件时 - java.io.FileNotFoundException when I'm trying to read into a binary file in my Android Studio project 如何使用getResourceAsStream读取XML文件 - How to read a XML file using getResourceAsStream getResourceAsStream() - InputStream 使用相对路径时为 null - getResourceAsStream() - InputStream null when using relative path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM