The properties file is often used in Java and parsed by using the properties class. Here, the benefits of using the properties file are not stated, and we mainly discuss how to use the properties file in Android!
Method 1: Because the properties file is finally read through the stream file, it is natural that we want to put the file into the assets folder or raw folder.
For example, we have a file --> test. properties. If it is placed in the assets folder, you can open it as follows:
Properties pro = new properties ();
Inputstream is = context. getassets (). Open ("test. properties ");
Pro. Load (is );
You can open the raw folder as follows:
Inputstream is = context. getresources (). openrawresource (R. Raw. test );
However, sometimes we don't want to get the context of Android, but load the properties file like the standard method in Java. How can we use it? See Method 2:
Method 2: loading without context! [Recommended Methods]
Properties pro = new properties ();
Pro. Load (fileload. Class. getresourceasstream ("test. properties "));
Fileload is the class name of the class where the loading method is located. The path where test. properties is stored is the same as fileload. java.
However, in Android, when we package and generate an APK and put it on a real mobile phone, you will not find test. the properties file should not be surprised. The resource files in android can only be stored in the assets or res sub-directories. The resource files in the package will be lost after compilation! So is our second method unusable? Of course not. After experiment, we found that you can still reference the file by placing the file in the assets folder and entering the absolute path of the file in the input path.
Code: Pro. Load (fileload. Class. getresourceasstream ("/assets/test. properties "));
Note: The root path of the Project path is "/"