Android phone SD card read and write operation (in txt text as an example) to achieve the steps _android

Source: Internet
Author: User
Tags stringbuffer
1, first to manifest registered SD card read and Write permissions
To illustrate, I'm not using Mainactivity.class as a software portal here.
Copy Code code as follows:

Androidmanifest.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "COM.TES.TEXTSD"
Android:versioncode= "1"
Android:versionname= "1.0" >
<uses-sdk
Android:minsdkversion= "8"
Android:targetsdkversion= "/>"
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
Android:allowbackup= "true"
android:icon= "@drawable/ic_launcher"
Android:label= "@string/app_name"
Android:theme= "@style/apptheme" >
<activity
Android:name= "Com.tes.textsd.FileOperateActivity"
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>
</manifest>

2. Create a class to read and write files in SD card
Copy Code code as follows:

Filehelper.java
/**
* @Title: Filehelper.java
* @Package COM.TES.TEXTSD
* @Description: TODO (describe what the file does with a word)
* @author alex.z
* @date 2013-2-26 5:45:40
* @version V1.0
*/
Package COM.TES.TEXTSD;
Import Java.io.DataOutputStream;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import Java.io.FileWriter;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Android.content.Context;
Import android.os.Environment;
public class Filehelper {
private context;
Does the/** SD card exist **/
Private Boolean HASSD = false;
/** SD card Path **/
Private String Sdpath;
/** the current package path **/
Private String Filespath;
Public Filehelper {
This.context = context;
HASSD = Environment.getexternalstoragestate (). Equals (
Android.os.Environment.MEDIA_MOUNTED);
Sdpath = Environment.getexternalstoragedirectory (). GetPath ();
Filespath = This.context.getFilesDir (). GetPath ();
}
/**
* Create a file on the SD card
*
* @throws IOException
*/
Public File createsdfile (String fileName) throws IOException {
File File = new file (Sdpath + "//" + fileName);
if (!file.exists ()) {
File.createnewfile ();
}
return file;
}
/**
* Delete the files on the SD card
*
* @param fileName
*/
public boolean deletesdfile (String fileName) {
File File = new file (Sdpath + "//" + fileName);
if (file = = NULL | |!file.exists () | | file.isdirectory ())
return false;
return File.delete ();
}
/**
* Write content to txt text in SD card
* Str as content
*/
public void Writesdfile (String str,string fileName)
{
try {
FileWriter FW = new FileWriter (Sdpath + "//" + fileName);
File F = new file (Sdpath + "//" + fileName);
Fw.write (str);
FileOutputStream OS = new FileOutputStream (f);
DataOutputStream out = new DataOutputStream (OS);
Out.writeshort (2);
Out.writeutf ("");
System.out.println (out);
Fw.flush ();
Fw.close ();
SYSTEM.OUT.PRINTLN (FW);
catch (Exception e) {
}
}
/**
* Read the SD card text file
*
* @param fileName
* @return
*/
public string Readsdfile (string fileName) {
StringBuffer sb = new StringBuffer ();
File File = new file (Sdpath + "//" + fileName);
try {
FileInputStream fis = new FileInputStream (file);
int C;
while ((c = fis.read ())!=-1) {
Sb.append ((char) c);
}
Fis.close ();
catch (FileNotFoundException e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
return sb.tostring ();
}
Public String Getfilespath () {
return filespath;
}
Public String Getsdpath () {
return sdpath;
}
public Boolean HASSD () {
return HASSD;
}
}

3. Write a layout to detect the function of reading and writing
Copy Code code as follows:

Main.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >
<textview
Android:id= "@+id/hassdtextview"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Hello"/>
<textview
Android:id= "@+id/sdpathtextview"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Hello"/>
<textview
Android:id= "@+id/filespathtextview"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Hello"/>
<textview
Android:id= "@+id/createfiletextview"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:text= "false"/>
<textview
Android:id= "@+id/readfiletextview"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:text= "false"/>
<textview
Android:id= "@+id/deletefiletextview"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:text= "false"/>
</LinearLayout>

4, is the UI of the class
Copy Code code as follows:

Fileoperateactivity.class
/**
* @Title: Fileoperateactivity.java
* @Package COM.TES.TEXTSD
* @Description: TODO (describe what the file does with a word)
* @author alex.z
* @date 2013-2-26 5:47:28
* @version V1.0
*/
Package COM.TES.TEXTSD;
Import java.io.IOException;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.TextView;
public class Fileoperateactivity extends activity {
Private TextView Hassdtextview;
Private TextView Sdpathtextview;
Private TextView Filespathtextview;
Private TextView Createfiletextview;
Private TextView Readfiletextview;
Private TextView Deletefiletextview;
Private Filehelper Helper;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Hassdtextview = (TextView) Findviewbyid (R.id.hassdtextview);
Sdpathtextview = (TextView) Findviewbyid (R.id.sdpathtextview);
Filespathtextview = (TextView) Findviewbyid (R.id.filespathtextview);
Createfiletextview = (TextView) Findviewbyid (R.id.createfiletextview);
Readfiletextview = (TextView) Findviewbyid (R.id.readfiletextview);
Deletefiletextview = (TextView) Findviewbyid (R.id.deletefiletextview);
Helper = new Filehelper (Getapplicationcontext ());
Hassdtextview.settext ("SD card exists:" + HELPER.HASSD ());
Sdpathtextview.settext ("SD card path:" + Helper.getsdpath ());
Filespathtextview.settext ("Package path:" + Helper.getfilespath ());
try {
Createfiletextview.settext ("Create File:"
+ Helper.createsdfile ("Test.txt"). GetAbsolutePath ());
catch (IOException e) {
E.printstacktrace ();
}
Deletefiletextview.settext ("Delete file succeeded:"
+ Helper.deletesdfile ("Xx.txt"));
Helper.writesdfile ("1213212", "test.txt");
Readfiletextview.settext ("Read file:" + helper.readsdfile ("test.txt"));
}
}

Look at the effect of the operation:
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.