Android uses the path api to internally store read and write files. androidapi
Copy and modify the original project
Copy the previously created Project CC + CV operation
To be modified:
* Project name
* Application package name
* Re-import the R file package
Next, modify the package name in the file/AndroidManifest. xml: package = "com. wuyudong. rwinrom"
For simplicity, change to package = "com. wuyudong. rwinrom2"
An error is reported in the R of MainActivity. java. You can simply point to the current package.
Modify app_name in strings. xml.
<String name = "app_name"> use the path api to store read/write files internally </string>
Use path api to read and write files
The path of the file object obtained by getFilesDir () is data/com. wuyudong. rwinrom2/files. The file stored in this path will remain
Public void saveAccount (String name, String pass) {// get a file object with the path data/com. wuyudong. rwinrom2/files File file = new File (getFilesDir (), "info.txt"); // File file = new File ("data/com. wuyudong. rwinrom/info.txt "); try {FileOutputStream fos = new FileOutputStream (file); fos. write (name + "#" + pass ). getBytes (); fos. close ();} catch (Exception e) {e. printStackTrace ();}}
The running result is as follows:
The file is generated successfully, and the downloaded content is correct.
* The path of the file object obtained by getCacheDir () is data/com. wuyudong. rwinrom2/cache.
Public void saveAccount (String name, String pass) {// get a file object with the path data/com. wuyudong. rwinrom2/files // File file = new File (getFilesDir (), "info.txt"); // you can get a file object named data/com. wuyudong. rwinrom2/cache File file = new File (getCacheDir (), "info.txt"); try {FileOutputStream fos = new FileOutputStream (file); fos. write (name + "#" + pass ). getBytes (); fos. close ();} catch (Exception e) {e. printStackTrace ();}}
Note: files stored in this path may be deleted when the memory is insufficient.
The clear cache on the system management application interface clears the items in the cache folder, clears the data, and clears the items in the entire package name directory.
Click "Clear cache" to clear the content in the cache folder.
If you click "Clear data", the folder under data/com. wuyudong. rwinrom2/will be cleared.