1.
Originally wanted to use the way in Java, the Internet to find some information, my test code is as follows:
1String str = system.getproperty ("Java.class.path");2String Strusrdir = System.getproperty ("Usr.dir");3URL url = This. GetClass (). GetResource (""); **test1.class is Class.forName ("Ddhxd.test.Test1")4 5MsgAppendLn01 ("Java.class.path:" +str);6MsgAppendLn01 ("Usr.dir:" +strusrdir);7 if(url = =NULL)8MsgAppendLn01 ("This.getclass (). getresource (\" \ ") return null.");9 ElseTenMsgAppendLn01 ("This.getclass (). getresource (\" \ "):" +url.tostring ());
However, the printed result is incorrect:
Java.class.path:.
Usr.dir:null
This.getclass (). GetResource ("") return null.
2.
Online search of the way Android, recorded as follows:
2.1.
Http://blog.sina.com.cn/s/blog_681929ae0101735n.html
Get Current Program Path
Getapplicationcontext (). Getfilesdir (). GetAbsolutePath (); Gets the installation package path for the program, String Path=getapplicationcontext (). Getpackageresourcepath (); Get the program default database pathGetapplicationcontext (). Getdatabasepath (s). GetAbsolutePath (); 2.2, Http://zhidao.baidu.com/link?url=A44Z23-lSQkEzqcyshselOpXbWdNJi6JrItfj6T6LCnsbgi_ Z6rrznlasjbfjhcwvb0331agziy9jvbm3gk9ol0rfkmziw8zdkpgaxl5mc7 Method Introduction:
Each Android application can get the application-related directory through the context, the functions of these directories are different, each directory has its own characteristics, and sometimes confusing, this article combined with the Android source code comments and the actual operation, the detailed introduction of each method:
Method: Getfilesdir
Explanation: Returns the absolute path to the file system created and stored by Context.openfileoutput (), application files, which are all deleted when the program is uninstalled.
Method: Getcachedir
Interpretation: Returns the cache directory specified by the application, which is removed when the device is out of memory, so the files stored here are not guaranteed and may be discarded at any time.
Method: Getdir
Explanation: This is a file that can store your own application customization, you can create or access this directory by using the file instance returned by this method, note that the files under that directory are accessible only by your own program.
Method: Getexternalcachedir
Interpretation: Use this method to write external storage permissions "<uses-permission android:name=" Android.permission.WRITE_EXTERNAL_STORAGE "/>", Calling this method returns the absolute path to the application's external file system (Environment.getexternalstoragedirectory ()) directory, which is the cache file used to store the application, as it does with the Getcachedir directory. The files in the directory will be erased when the program is uninstalled.
Method: Getexternalfilesdir
Interpretation: Use this method to write external storage permissions "<uses-permission android:name=" Android.permission.WRITE_EXTERNAL_STORAGE "/>", This directory is an external file system associated with the application, and it is not the same as Getexternalcachedir, as long as the application exists it will always exist, these files belong to your application, can not be accessed by others. Similarly, files in this directory are deleted when the program is uninstalled.
Method: Getexternalfilesdir
Interpretation: As in the above method, just return a file of a certain type under its directory, these types can be: Environment#directory_music Music environment#directory_podcasts Audio Environment #DIRECTORY_RINGTONES ringtone environment#directory_alarms alarm environment#directory_notifications notification ringtone environment# Directory_pictures Picture Environment#directory_movies Video
Method: Getdatabasepath
Explanation: Saving a database file created through Context.openorcreatedatabase
Method: Getpackagecodepath
Explanation: Returns the full path of the Android installation package, which is a zip file containing the application code and the assets file.
Method: Getpackageresourcepath
Explanation: Returns the full path of the Android installation package, which is a zip file to lock, which includes the application's private resources.
Method: Getobbdir
Explanation: Return the application's Obb file directory (if any), note that if the application does not have any OBB files, this directory does not exist.
Second, the test procedure:
The test code is as follows:
Private StringBuilder GetFilePath () {
StringBuilder Filepathbuilder = new StringBuilder ();
Returns the absolute path to the file system created and stored by Context.openfileoutput (), application files, which are all deleted when the program is uninstalled.
Filepathbuilder.append ("Getfilesdir = ="). Append (Getfilesdir ()). Append ("\ n");
Returns the cache directory specified by the application, which takes precedence when the device is out of memory, so the files stored here are not guaranteed and may be discarded at any time.
Filepathbuilder.append ("Getcachedir = ="). Append (Getcachedir ()). Append ("\ n");
This is a file that can store your own application customization, you can create or access this directory by using the file instance returned by this method, note that the files under that directory are accessible only by your own program.
Filepathbuilder.append ("Getdir = ="). Append (Getdir ("Test.txt", context.mode_world_writeable)). Append ("\ n");
/* Need to write file permissions <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> */
Calling this method returns the absolute path to the application's external file system (Environment.getexternalstoragedirectory ()) directory, which is the cache file used to store the application, as it does with the Getcachedir directory. The files in the directory will be erased when the program is uninstalled.
Filepathbuilder.append ("Getexternalcachedir = ="). Append (Getexternalcachedir ()). Append ("\ n");
This directory is an external file system associated with the application, and it is not the same as Getexternalcachedir, as long as the application exists it will always exist, these files belong to your application, can not be accessed by others. Similarly, files in this directory are deleted when the program is uninstalled.
Filepathbuilder.append ("Getexternalfilesdir = ="). Append (Getexternalfilesdir ("/")). Append ("\ n");
/**
* As in the above method, just return a file of a certain type under its directory, these types can be:
* Environment#directory_music Music
* Environment#directory_podcasts Audio
* Environment#directory_ringtones Ringtones
* Environment#directory_alarms Alarm
* Environment#directory_notifications Notification ringtone
* Environment#directory_pictures Pictures
* Environment#directory_movies Video
*
* */
Filepathbuilder.append ("Getexternalfilesdir = ="). Append (Getexternalfilesdir (environment.directory_pictures)). Append ("\ n");
To save a database file created through Context.openorcreatedatabase
Filepathbuilder.append ("Getdatabasepath = ="). Append (Getdatabasepath (Data_base_name)). Append ("\ n");
Returns the full path to the Android installation package, which is a zip archive that includes the application code and the assets file
Filepathbuilder.append ("Getpackagecodepath = ="). Append (Getpackagecodepath ()). Append ("\ n");
Returns the full path to the Android installation package, which is a zip file to lock, which includes the application's private resources.
Filepathbuilder.append ("Getpackageresourcepath = ="). Append (Getpackageresourcepath ()). Append ("\ n");
Return to the application's Obb file directory (if any), note that if the application does not have any OBB files, this directory does not exist.
Filepathbuilder.append ("Getobbdir = ="). Append (Getobbdir ()). Append ("\ n");
return filepathbuilder;
}
X
Get Current Program Path