Android Development stores data to file instances

Source: Internet
Author: User
Tags readfile

The first step, rewrite Androidmanifest.xml, give the mobile phone SCard card Authorization

<!--add SCard card Read and write authorization-->
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

Then add unit test instrumentation

<instrumentation
Android:name= "Android.test.InstrumentationTestRunner"
Android:targetpackage= "Com.example.androidsdcard" >
</instrumentation>

And under the application, add <uses-library android:name= "Android.test.runner"/>


<application
Android:allowbackup= "true"
android:icon= "@drawable/ic_launcher"
Android:label= "@string/app_name"
Android:theme= "@style/apptheme" >
<uses-library android:name= "Android.test.runner"/>
<activity
Android:name= ". Mainactivity "
Android:label= "@string/app_name" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>

<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>


The second step is to write the FileStream of writing the document

Package Com.example.androidsdcard;

Import Java.io.ByteArrayOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.OutputStream;

Import Android.content.Context;
Import android.os.Environment;

public class FileStream {
private context;

Public FileStream {
This.context = context;
}

Public FileStream () {

}
Read the contents of the SD card file
public string ReadFile (string filename) {
FileInputStream Inputstream=null;
Character Buffer stream
Bytearrayoutputstream outputstream=new Bytearrayoutputstream ();
Get the path to the SD card via environment
File File=new file (environment.getexternalstoragedirectory (), filename);
To determine whether SDcard exists
if (Environment.MEDIA_MOUNTED.equals (Environment.getexternalstoragestate ())) {
try {
Inputstream=new fileinputstream (file);
int len=0;
Creating a byte array
Byte[]data=new byte[2048];
Read by size of byte array
while ((Len=inputstream.read (data))!=-1) {
Outputstream.write (data, 0, Len);
}
catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
if (inputstream!=null) {
try {
Inputstream.close ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}
return new String (Outputstream.tobytearray ());

}

public boolean savafile (string filename, string content) {
Boolean flag = false;
FileOutputStream outputstream = null;
File File = new file (Environment.getexternalstoragedirectory (),
filename);
if (Environment.MEDIA_MOUNTED.equals (environment
. Getexternalstoragestate ()) {
try {
OutputStream = new FileOutputStream (file);
Outputstream.write (Content.getbytes ());
Flag = true;
catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
finally {
if (OutputStream!= null) {
try {
Outputstream.close ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}

}

return flag;
}

}


Part III, write test class for unit test, read and write files

Package Com.example.androidsdcard;

Import Android.content.Context;
Import Android.test.AndroidTestCase;
Import Android.util.Log;

public class MyTest extends Androidtestcase {
Private final String tag= "MyTest";
Public MyTest () {

}
public void SaveFile () {//test file Write
Context Context=getcontext ();
FileStream filestream=new FileStream (context);
Boolean Flag=filestream.savafile ("Hello.txt", "How are You?") ");
LOG.I (TAG, "--->" +flag);

}
public void ReadFile () {//test file content Read
Context Context=getcontext ();
FileStream filestream=new FileStream (context);
String filename= "Hello.txt";
String flag=filestream.readfile (filename);
LOG.I (TAG, "--->" +flag);

}

}

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.