2013년 6월 18일 화요일

Android에서 java.util.Properties 사용하기

이전 블로그에서 이전 함 (원본 글 2013/06/18 작성)

Android에서 Properties를 사용할 필요가 있어 확인해 보니..


Asset Manager를 통해서 접근하는 방법과 res로 접근하는 방법이 있었음.
첫번째로 하긴 했는데.. 두번째는 안해봐서 ㅎㅎ

 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();
}
The code is dead simple and thank God for the Properties class, although microproperties would do the trick. The second way to do it is as simple as the first approach. The code looks like this:
// Read from the /res/raw directory
try {
    InputStream rawResource = resources.openRawResource(R.raw.micrologv2);
    Properties properties = new Properties();
    properties.load(rawResource);
    System.out.println("The properties are now loaded");
    System.out.println("properties: " + properties);
} catch (NotFoundException e) {
    System.err.println("Did not find raw resource: "+e);
} catch (IOException e) {
    System.err.println("Failed to open microlog property file");
}

댓글 없음:

댓글 쓰기