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 ->, if put into the assets folder, you can open the following

Properties pro = new Properties ();

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

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 (""));

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

However, in android, when we generate apk package, it will put apk to a real phone, you will not find 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 /"));

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 人气:138
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. 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

  • defined in the i18n resource file 2009-08-16

    Q: defined i18n resource file, can be divided into several modules according to the system resource file, not all written in one instance 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 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 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C), All Rights Reserved. 版权所有 闽ICP备15018612号

processed in 0.146 (s). 12 q(s)