Case Study on reading and writing android development files

Source: Internet
Author: User

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 Copy codeThe Code is as follows :/**
* 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
{
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 ();
}
/**
* Save the file to the SD card.
*
* @ 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
*
* @ Param filename: File Name
* @ Return
*/
Public String read (String filename) throws Exception
{
FileInputStream FCM = context. openFileInput (filename );
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;
}
}

MainActivityCopy codeThe Code is as follows: 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. xmlCopy codeThe Code is as follows: <! -- 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.