to read properties file android

java properties frequently used files, and then by using the Properties class to parse, and is not state the benefits of using the properties file, mainly on how to use properties file in the android!

Method One: because in the end flow of documents through the properties file to be read, so naturally, we think of assets to file into the folder or folder in the raw.

For example, here we have a file -> test.properties, if put into the assets folder, you can open the following

Properties pro = new Properties ();

InputStream is = context.getAssets (). Open ("test.properties");

pro.load (is);

If put to the raw folder, you can open the following

InputStream is = context.getResources (). OpenRawResource (R.raw.test);

However, sometimes we do not want to get android context (context), but as the standard method of loading java properties file, then how do we use it? See Method Two:

Method Two: There is no context to load! [Recommend Us]

Properties pro = new Properties ();

pro.load (FileLoad.class.getResourceAsStream ("test.properties"));

Which, FileLoad loading method is the class where the class name. test.properties the path stored in the same package with FileLoad.java.

However, in android, when we generate apk package, it will put apk to a real phone, you will not find test.properties file, do not be surprised, android in the resource file is only stored in the assets or res subdirectories inside the package in the resource file is compiled, will be lost! It is not our second method would not be able to use it? Of course not, after the experiments showed that the file into the assets folder, fill in the incoming path of the file inside the absolute path, or can refer to the file.

Code: pro.load (FileLoad.class.getResourceAsStream ("/ assets / test.properties"));

Note: The path to the root of the project which is "/"

This way, you can successfully use your Properties files! o (∩ ∩) o. .. haha

分类:Mobile 时间:2010-11-30 人气:134
分享到:
blog comments powered by Disqus

相关文章

  • linux shell access to the file absolute path. 2010-11-06

    To get the program running / scripts stored in their own absolute path, which can be used in PHP dirname (realpath (__FILE__)); C #, there System.Windows.Forms.Application.StartupPath; java seems to be no more direct methods, can only be obtained ind

  • Java relative path / absolute path summary (change) 2010-05-06

    Java code 1. Basic understanding of the concept Absolute path: absolute path is your home page file or directory on your hard disk real path, (URL and the physical path) example: <DIV Class =quote_title> quote </ DIV> <DIV class =quote_div&

  • Java relative path / absolute path to the Summary (rpm) 2010-09-27

    Absolute path: absolute path is your home files or directories on the real path on your hard drive, (URL and the physical path) example: C: xyz est.txt represents the absolute path of the file test.txt. http://www.sun.com/index.htm also represents th

  • java class to get the absolute path of the current 2011-08-03

    Jsp and class files in relative path to call different. In the jsp in the root directory is WebRoot in the class file, the root directory WebRoot / WEB-INF / classes, of course you can use System.getProperty ("user.dir") to obtain the absolute p

  • struts.properties defined in the i18n resource file 2009-08-16

    Q: struts.properties defined i18n resource file, can be divided into several modules according to the system resource file, not all written in one instance messageResource.properties in??? -------------------------------------------------- ----------

  • JAR package for Java, or the path to read the resource file 2010-07-26

    Here are divided into two specific: The first: the resource file for the general suffix file The second: Resource file for picture files First] [NO1 Use this line of code can get class path of the root class String path = Thread.currentThread (). Get

  • php file for the current absolute path 2010-08-30

    1.dirname (__FILE__) function returns the path of the script is located in the <? Php $ Basedir = dirname (__FILE__); echo $ basedir; / / Will print out a page where the absolute path of this file! ?> Can be used after define ('ROOT', dirname (__FIL

  • log4j log file relative path in the configuration method 2010-12-21

    Reprinted from http://www.zxbc.cn/html/20080402/33022_4.html Natural solution is to use relative paths instead of absolute path, the following three methods: Method one, so log4j to read from the System attribute values corresponding to the property'

  • Read the package resource file or image path 2010-03-29

    When we written a java application, the jar will be labeled as files, double-click to run, but in the IDE or resources used in a picture file, in the development phase is normal, but the labeled jar, after which they reported anomalies, and File Not

  • Struts1 Source analysis - international resource file 2010-04-01

    Struts1 Source analysis - international resource file 1. URL class diagram 2. Description a) Struts resources to international, use the JDK following categories: o java.text.MessageFormat o java.util.ResourceBundle o java.util.PropertyResourceBundle

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.178 (s). 14 q(s)