1

From this question, I know how to obtain Properties within an Android Application.

However, if I want to write an Android Library, and intend to read a properties file (say myconfig.properties, how can I do so?

Given that the Android Library (e.g. it's called "myAndroidLib" ) has android-support-v4.jar , and this library is imported to an Android Application. Also given that myconfig.properties exists.

I found a piece of code in the Internet, but this works only within Android:

Resources resources = this.getResources();
AssetManager assetManager = resources.getAssets();

// Read from the /assets directory
try {
    InputStream inputStream = assetManager.open("microlog.properties");
    Properties properties = new Properties();
    properties.load(inputStream);
    System.out.println("The properties are now loaded");
    System.out.println("properties: " + properties);
} catch (IOException e) {
    System.err.println("Failed to open microlog property file");
    e.printStackTrace();
}
4

1 回答 1

2

我在互联网上找到了一段代码,但这仅适用于Android

该代码将在 Android 库项目中正常工作。您将需要托管应用程序为您提供Context最终可以从中派生AssetManager.

于 2013-05-28T09:56:48.410 回答