Android Development file read and write application case Analysis _android

Source: Internet
Author: User
Tags save file
I. Basic Concepts
Save a file in an Android application and save it in two places
① mobile phone with the storage space, small (such as 200M), suitable for saving some small files, Android save location in the data/data/application package name/files directory
② external storage devices such as SD cards, large, suitable for saving large files such as video, Android save location in Mnt/sdcard directory, androd1.5,android1.6 save in SDcard directory
Saved locations can be found by using the Android File Explorer view

Second, examples
Copy Code code as follows:

/**
* File Action Class
*
* @author XY
*
*/
public class Fileservice
{
/**
* Context Object
*/
private context;
Public Fileservice
{
Super ();
This.context = context;
}
/**
* Save the file to the phone's own storage space
*
* @param filename filename
* @param filecontent file contents
*/
@SuppressWarnings ("Static-access")
public void Save (string filename, String filecontent) throws Exception
{
FileOutputStream fos = context.openfileoutput (filename, context.) Mode_private); Default storage space saved on phone
Fos.write (Filecontent.getbytes ("UTF-8"));
Fos.close ();
}
/**
* Save file to sd card
*
* @param filename filename
* @param filecontent file contents
*/
public void Saveinsdcard (string filename, String filecontent) throws Exception
{
If the file is saved in SDcard, the file is not read-write control
File File = new file (environment.getexternalstoragedirectory (), filename);
FileOutputStream fos = new FileOutputStream (file);
Fos.write (Filecontent.getbytes ("UTF-8"));
Fos.close ();
}
/**
* Read the contents of the file
*
* @param filename filename
* @return
*/
Public String Read (String filename) throws Exception
{
FileInputStream FIS = context.openfileinput (filename);
Bytearrayoutputstream OutStream = new Bytearrayoutputstream ();
byte[] buffer = new byte[1024];
int len = 0;
Read the contents to the buffer and read to the end-1
while (len = fis.read (buffer))!=-1)
{
This example writes the contents of a byte array (buffer variable) to the memory buffer every time it is read to save the contents of each
Outstream.write (buffer, 0, Len);
}
Take the data saved in memory
byte[] data = Outstream.tobytearray ();
Fis.close ();
string result = new String (data, "UTF-8");
return result;
}
}

Mainactivity
Copy Code code as follows:

Try
{
stored in the cell phone with its own storage space
Fs.save (filename, filecontent);
Toast.maketext (Getapplicationcontext (), r.string.success, Toast.length_short). Show ();
stored in an external device SD card
Determine if sdcard exists and is readable or writable
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 ());
}

File name without path, input directly as Xy.txt
For SD card operations, you need to add permissions to the Androidmanifest.xml
Copy Code code as follows:

<!--permissions to create and delete files in SDcard-->
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!--permissions to write data to SDcard-->
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>


three or one more APIs
①environment.getexternalstoragedirectory () Gets the path to the Mnt/sdcard directory, and for the android1.5,1.6 path is the SDcard directory
Two APIs are available in ②activity
Getcachedir () Gets the path to the data/data/application package name/cache directory
Getfilesdir () Gets the path to the data/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.