Android implements SD card and memory file storage in the same way. The method for obtaining the file path is basically the same as that of java. The following is the code of the program. The configuration location and implementation are different. Others are the same. The following code is used:
Main. xml:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<EditText android: id = "@ + id/edit1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: lines = "4"/>
<Button android: id = "@ + id/write"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/write"/>
<EditText android: id = "@ + id/edit2"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: editable = "false"
Android: cursorVisible = "false"
Android: lines = "4"/>
<Button android: id = "@ + id/read"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/read"/>
</LinearLayout>
SD card reading must be supported in the AndroidManifest list file. The Code is as follows:
[Html]
<Uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"/>
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
Add two contents to String. xml:
[Html]
<String name = "read"> read </string>
Lt; string name = "write"> write </string>
The code for file implementation is as follows:
[Java]
Package cn. jason. io;
Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. PrintStream;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
Public class FileTestActivity extends Activity {
Final String FILE = "jason. bin ";
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Button read = (Button) findViewById (R. id. read );
Button write = (Button) findViewById (R. id. write );
Final EditText edit1 = (EditText) findViewById (R. id. edit1 );
Final EditText edit2 = (EditText) findViewById (R. id. edit2 );
Write. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Write (edit1.getText (). toString ());
Edit1.setText ("");
}
});
Read. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Edit2.setText (read ());
}
});
}
Private String read (){
Try {
FileInputStream FCM = openFileInput (FILE );
Byte [] buffer = new byte [1024];
Int hasRead = 0;
StringBuilder sb = new StringBuilder ("");
While (hasRead = FS. read (buffer)> 0 ){
Sb. append (new String (buffer, 0, hasRead ));
}
Return sb. toString ();
} Catch (IOException e ){
E. printStackTrace ();
}
Return null;
}
Private void write (String content)
{
Try
{
// Open the file output stream in append Mode
FileOutputStream fos = openFileOutput (FILE, MODE_APPEND );
// Package FileOutputStream into PrintStream
PrintStream ps = new PrintStream (fos );
// Output file content: www.2cto.com
Ps. println (content );
Ps. close ();
}
Catch (Exception e)
{
E. printStackTrace ();
}
}
}
The following code implements the SD card storage:
[Java]
Package cn. jason. io;
Import java. io. BufferedReader;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileNotFoundException;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import java. io. RandomAccessFile;
Import java.net. ContentHandler;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. OS. Environment;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
Public class SDCardTestActivity extends Activity {
Final String FILE = "/jason. bin ";
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Button read = (Button) findViewById (R. id. read );
Button write = (Button) findViewById (R. id. write );
// Obtain two text boxes
Final EditText edit1 = (EditText) findViewById (R. id. edit1 );
Final EditText edit2 = (EditText) findViewById (R. id. edit2 );
// Bind the event listener to the write button
Write. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View source ){
// Write the content in edit1 to the file
Write (edit1.getText (). toString ());
Edit1.setText ("");
}
});
Read. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// Read and display the content in the specified file
Edit2.setText (read ());
}
});
}
Private String read (){
// If the mobile phone is inserted with an SD card and the application has the permission to access the SD card
Try {
If (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED )){
// Obtain the directory of the SD card
File sdDirFile = Environment. getExternalStorageDirectory ();
// Obtain the input stream corresponding to the specified file
FileInputStream FCM = new FileInputStream (sdDirFile. getCanonicalPath () + FILE );
// Package the specified input stream into BufferedReader
BufferedReader br = new BufferedReader (new InputStreamReader (FCM ));
StringBuilder sb = new StringBuilder ("");
String line = null;
While (line = br. readLine ())! = Null ){
Sb. append (line );
}
Return sb. toString ();
}
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
Return null;
}
Private void write (String context ){
Try {
If (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED )){
File sdDir = Environment. getExternalStorageDirectory ();
File targetFile = new File (sdDir. getCanonicalPath () + FILE );
RandomAccessFile raf = new RandomAccessFile (targetFile, "rw ");
Raf. seek (targetFile. length ());
Raf. write (context. getBytes ());
Raf. close ();
}
} Catch (Exception e ){
}
}
}
From Jason's Java Column