I. Basic Concepts
Save the file in the Android app in two places
① The storage space provided by the mobile phone is small (such as 200 MB) and suitable for saving some small files. The storage location in Android is in the data/application package name/files directory.
② External storage devices, such as SD cards, are large and suitable for storing large files, such as videos. They are stored in the mnt/sdcard directory in Android and stored in the sdcard directory in androd1.5 and android1.6.
The saved location can be found in the file explorer view of android.
Ii. Example
- Package cn. xy. service;
-
- Import java. io. ByteArrayOutputStream;
- Import java. io. File;
- Import java. io. FileInputStream;
- Import java. io. FileOutputStream;
-
- Import android. content. Context;
- Import android. OS. Environment;
-
- /**
- * File Operations
- *
- * @ Author XY
- *
- */
- Public class FileService
- {
- /**
- * Context object
- */
- Private Context context;
-
- Public FileService (Context context)
- {
- Super ();
- This. context = context;
- }
-
- /**
- * Save the file to the bucket that comes with your mobile phone)
- *
- * @ Param filename: File Name
- * @ Param fileContent File Content
- */
- @ SuppressWarnings ("static-access ")
- Public void save (String filename, String fileContent) throws Exception
- {
- // Private operation mode: 1. This file can only be accessed by this application. 2. The content written to the file will overwrite the content of the original file.
- FileOutputStream fos = context. openFileOutput (filename, context. MODE_PRIVATE); // It is stored in the bucket of the mobile phone by default.
- Fos. write (fileContent. getBytes ("UTF-8 "));
- Fos. close ();
- }
-
- /**
- * SD card for saving files
- *
- * @ Param filename: File Name
- * @ Param fileContent File Content
- */
- Public void saveInSDCard (String filename, String fileContent) throws Exception
- {
- // If the object is saved in SDCard, the object is not read/write controlled.
- File file = new File (Environment. getExternalStorageDirectory (), filename );
- FileOutputStream fos = new FileOutputStream (file );
- Fos. write (fileContent. getBytes ("UTF-8 "));
- Fos. close ();
- }
-
- /**
- * Reading file content
- * When you create a ByteArrayOutputStream class instance, a byte array buffer is created in the memory. The buffer increases automatically as data is constantly written.
- * Get data from toByteArray () and toString (). Disable ByteArrayOutputStream.
- * Invalid. Methods in this class can still be called after the stream is closed without generating any IOException.
- *
- * @ Param filename: File Name
- * @ Return
- */
- Public String read (String filename) throws Exception
- {
- FileInputStream FCM = context. openFileInput (filename); // by default, it is found in the bucket of the mobile phone.
- ByteArrayOutputStream outStream = new ByteArrayOutputStream ();
- Byte [] buffer = new byte [1024];
- Int len = 0;
- // Read the content to the buffer. The value is-1 at the end.
- While (len = FS. read (buffer ))! =-1)
- {
- // In this example, the content of each read byte array (buffer variable) is written to the memory buffer to save each content.
- OutStream. write (buffer, 0, len );
- }
- // Retrieve the data stored in the memory
- Byte [] data = outStream. toByteArray ();
- FCM. close ();
- String result = new String (data, "UTF-8 ");
- Return result;
- }
-
- }
MainActivity
- Try
- {
- // The bucket stored in the mobile phone
- Fs. save (filename, fileContent );
- Toast. makeText (getApplicationContext (), R. string. success, Toast. LENGTH_SHORT). show ();
-
- // SD card stored on external devices
- // Determine whether an SDCARD exists and whether it can be read/written
- If (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED ))
- {
- Fs. saveInSDCard (filename, fileContent );
- Toast. makeText (getApplicationContext (), R. string. success, Toast. LENGTH_SHORT). show ();
- }
- Else
- {
- Toast. makeText (getApplicationContext (), R. string. failsdcard, Toast. LENGTH_SHORT). show ();
- }
-
- }
- Catch (Exception e)
- {
- Toast. makeText (getApplicationContext (), R. string. fail, Toast. LENGTH_SHORT). show ();
- Log. e (tag, e. getMessage ());
- }
The file name is input directly, such as xy.txt.
For SD card operations, you need to add permissions in AndroidManifest. xml
- <! -- Create and delete object permissions in SDCard -->
- <Uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"/>
-
- <! -- Write data to SDCard -->
- <Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
Iii. APIs
① The Path obtained by Environment. getExternalStorageDirectory () is the mnt/sdcard directory. For android1.5, 1.6 is the sdcard directory.
② Activity provides two APIs
The Path obtained by getCacheDir () is data/application package name/cache directory.
The Path obtained by getFilesDir () is data/application package name/files directory.
This article from the "IT Xu fat column" blog, please be sure to keep this source http://woshixy.blog.51cto.com/5637578/1076826