1. First, register the SD card read and write permissions for manifest
I didn't use MainActivity. class as the software entry here.
Copy codeThe Code is 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" type = "codeph" text = "/codeph">
<Uses-sdk
Android: minSdkVersion = "8"
Android: targetSdkVersion = "16"/>
<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 for reading and writing files on the SD card Copy codeThe Code is as follows: FileHelper. java
/**
* @ Title: FileHelper. java
* @ Package com. tes. textsd
* @ Description: TODO (describe the file in one sentence)
* @ Author Alex. Z
* @ Date 5:45:40 pm
* @ 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 context;
/** Whether the SD card exists **/
Private boolean hasSD = false;
/** SD card path **/
Private String SDPATH;
/** Path of the current package **/
Private String FILESPATH;
Public FileHelper (Context context ){
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 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 the content to the txt text in the SD card.
* Str indicates the 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 text files from the SD card
*
* @ Param fileName
* @ Return
*/
Public String readSDFile (String fileName ){
StringBuffer sb = new StringBuffer ();
File file = new File (SDPATH + "//" + fileName );
Try {
FileInputStream FCM = new FileInputStream (file );
Int c;
While (c = FCM. read ())! =-1 ){
Sb. append (char) c );
}
FCM. 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 for detecting read/write Functions Copy codeThe Code is 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. It is the UI class. Copy codeThe Code is as follows: FileOperateActivity. class
/**
* @ Title: FileOperateActivity. java
* @ Package com. tes. textsd
* @ Description: TODO (describe the file in one sentence)
* @ Author Alex. Z
* @ Date 5:47:28 pm
* @ 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 ("Whether the 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 ("Whether the file is successfully deleted :"
+ Helper. deleteSDFile ("xx.txt "));
Helper. writeSDFile ("1213212", "test.txt ");
ReadFileTextView. setText ("Read File:" + helper. readSDFile ("test.txt "));
}
}
Check the running result: