This article describes the storage and reading functions of Android files. Share it for your reference, as follows:
Note: There are getFileDir() and getCacheDir(); methods in the Activity to get the path of the current package file in the storage space provided by the current mobile phone.
getFileDir() ----- /data/data/(current package)/files
getCacheDir() ----- /data/data/(current package)/cache
1. Write file read and write function implementation class FileService
package ; import ; import ; import ; import ; import ; /** * File saving and reading function implementation class * @author Administrator * * 2010-6-28 08:15:18 pm */ public class FileService { public static final String TAG = "FileService"; private Context context; //Get the reference to the incoming context object public FileService(Context context) { = context; } /** * Save the file * * @param fileName filename * @param content file content * @throws Exception */ public void save(String fileName, String content) throws Exception { // Since the page inputs all text information, when the file name does not end with the .txt suffix, the .txt suffix will be automatically added. if (!(".txt")) { fileName = fileName + ".txt"; } byte[] buf = ("iso8859-1"); (TAG, new String(buf,"utf-8")); fileName = new String(buf,"utf-8"); (TAG, fileName); // Context.MODE_PRIVATE: It is the default operation mode, which means that the file is private data and can only be accessed by the application itself. In this mode, the written content will overwrite the content of the original file. If you want to append the newly written content to the original file. Context.MODE_APPEND can be used // Context.MODE_APPEND: The mode will check whether the file exists. If it exists, it will add content to the file, otherwise a new file will be created. // Context.MODE_WORLD_READABLE and Context.MODE_WORLD_WRITEABLE are used to control whether other applications have permission to read and write the file. // MODE_WORLD_READABLE: means that the current file can be read by other applications; MODE_WORLD_WRITEABLE: means that the current file can be written by other applications. // If you want the file to be read and written by other applications, you can pass it in: // openFileOutput("", Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE); FileOutputStream fos = (fileName, context.MODE_PRIVATE); (()); (); } /** * Read file contents * * @param fileName filename * @return File content * @throws Exception */ public String read(String fileName) throws Exception { // Since the page inputs all text information, when the file name does not end with the .txt suffix, the .txt suffix will be automatically added. if (!(".txt")) { fileName = fileName + ".txt"; } FileInputStream fis = (fileName); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int len = 0; //Put the read data in memory ---ByteArrayOutputStream while ((len = (buf)) != -1) { (buf, 0, len); } (); (); //Return the data stored in memory return (); } }
2. Write Activity class:
package ; import ; import ; import ; import ; import ; import ; import ; import ; public class TestAndroidActivity extends Activity { /** Called when the activity is first created. */ //Get FileService object private FileService fileService = new FileService(this); //Define the filename input box object in the view private EditText fileNameText; //Define the contentText input box object in the view private EditText contentText; //Define a toast prompt object private Toast toast; @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); //Get object references of two input boxes and two buttons in the view Button button = (Button)(); Button read = (Button)(); fileNameText = (EditText) (); contentText = (EditText) (); //Add a save event for the save button (new () { @Override public void onClick(View v) { String fileName = ().toString(); String content = ().toString(); //When the file name is empty, prompt the user to have the file name empty and record the log. if(isEmpty(fileName)) { toast = (, .empty_filename, Toast.LENGTH_LONG); (RESULT_CANCELED, 0.345f); (); (, "The file name is empty"); return; } //When the file content is empty, the user is prompted to be empty and the log is recorded. if(isEmpty(content)) { toast = (, .empty_content, Toast.LENGTH_LONG); (RESULT_CANCELED, 0.345f); (); (, "The file content is empty"); return; } //When the file name and content are not empty, call the save method of fileService // When the execution is successful, the user is prompted to save successfully and log the log //When an exception occurs, the user is prompted to fail to save and log try { (fileName, content); toast = (, , Toast.LENGTH_LONG); (RESULT_CANCELED, 0.345f); (); (, "The file save successful"); } catch (Exception e) { toast = (, , Toast.LENGTH_LONG); (RESULT_CANCELED, 0.345f); (); (, "The file save failed"); } } }); //Add a read event for the read button (new () { @Override public void onClick(View v) { //Get the value in the file name input box String fileName = ().toString(); //If the file name is empty, the user is prompted to enter the file name and record the log if(isEmpty(fileName)) { toast = (, .empty_filename, Toast.LENGTH_LONG); (RESULT_CANCELED, 0.345f); (); (, "The file name is empty"); return; } //Call the read method of fileService and put the read content into the text content input box //If the execution is successful, the user is prompted to read successfully and the log is recorded. //If an exception message appears (example: the file does not exist), the user will be prompted to fail to read and log the log. try { ((fileName)); toast = (, .read_success, Toast.LENGTH_LONG); (RESULT_CANCELED, 0.345f); (); (, "The file read successful"); } catch (Exception e) { toast = (, .read_fail, Toast.LENGTH_LONG); (RESULT_CANCELED, 0.345f); (); (, "The file read failed"); } } }); } //Write an isEmpty method to determine whether the string is empty private boolean isEmpty(String s) { if(s == null || "".equals(())) { return true; } return false; } }
3. File layout file:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/filename" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android: /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/content" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:minLines="3" android: /> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android: android:text="@string/save" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android: android:text="@string/read" /> </LinearLayout> </LinearLayout>
PS: When I was testing this function, I found that the file name cannot be used in Chinese (sdk2.2 + emulator), if there is any expert who accidentally browses this article and can give guidance on this issue, I will be grateful. hehe.
For more information about Android related content, please check out the topic of this site:Android file operation skills summary》、《Android programming activity operation skills summary》、《Android View View Tips Summary》、《Summary of Android's SQLite database skills》、《Summary of Android data skills for operating json format》、《Android database operation skills summary》、《A summary of SD card operation methods for Android programming and development》、《Android development introduction and advanced tutorial》、《Android resource operation skills summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.