Package com.atguigu.l04_datastorage;
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 android.app.Activity;
Import Android.os.Bundle;
Import android.os.Environment;
Import Android.view.View;
Import Android.widget.EditText;
Import Android.widget.Toast;
/**
* Test Phone external file storage
*
* @author Houzhiqiang
*
*/
public class Ofactivity extends Activity {
Private EditText Et_of_name;
Private EditText et_of_content;
@Override
protected void onCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_of);
Et_of_name = (EditText) Findviewbyid (r.id.et_of_name);
Et_of_content = (EditText) Findviewbyid (r.id.et_of_content);
}
public void Save (View v) throws IOException {
//1. Determine the status of the SD card, if the status is mounted to continue, otherwise prompt
if ( Environment.getexternalstoragestate (). Equals (environment.media_mounted) {
//2. Read the file name/contents of the input
String filename = Et_of_name.gettext (). toString ();
String content = Et_of_content.gettext (). toString ();
//3. Get outputstream for the specified file
//1). Get the files path under the SD card
String filespath = Getexternalfilesdir (null). GetAbsolutePath ();
//2). Form the full path
String FilePath = filespath+ "/" +filename;
3). Create FileOutputStream
FileOutputStream fos = new FileOutputStream (filePath);
4. Write Data
Fos.write (content.getbytes ("Utf-8"));
Fos.close ();
//5. Hint
Toast.maketext (this, "save Complete", 0). Show ();
} else {
Toast.maketext (this, "SD card is not mounted", 0). Show ();
}
}
public void Read (View v) throws Exception {
//1. Determine the status of the SD card, if the status is mounted to continue, otherwise prompt
if (environment.getexternalstor Agestate (). Equals (
environment.media_mounted)) {
//2. Read the file name of the input
String filename = Et_of_name.gettext (). ToString ();
//3. Get InputStream of the specified file
//1). Get the files path under the SD card
String filespath = Getexternalfilesdir (null). GetAbsolutePath ();
//2). Form the full path
String FilePath = Filespath + "/" + FileName;
3). Create FileInputStream
FileInputStream fis = new FileInputStream (filePath);
4. Reads the data into string
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
byte[] buffer = new byte[1024];
int len =-1;
while ((Len=fis.read (buffer))!=-1) {
Baos.write (buffer, 0, Len);
}
String content = baos.tostring ();
//5. Display
Et_of_content.settext (content);
} else {
Toast.maketext (this, "SD card not mounted", 0). Show ();
}
}
///storage/sdcard/atguigu/xxx.txt
public void Save2 (View v) throws IOException {
//1. Determine the status of the SD card, if the status is mounted to continue, Otherwise prompt
if (environment.getexternalstoragestate () equals (environment.media_mounted)) {
//2. Read the file name/contents of the input
String fileName = Et_of_name.gettext (). toString ();
String content = Et_of_content.gettext (). toString ();
//3. Get OutputStream of the specified file
//1)./storage/sdcard/
String Sdpath = Environment.getexternalstoragedirectory () . GetAbsolutePath ();
//2). /storage/sdcard/atguigu/(Create folder)
File File = new file (sdpath+ "/atguigu");
if (!file.exists ()) {
File.mkdirs ();//Create Folder
}
//3)./storage/sdcard/atguigu/xxx.txt
String FilePath = sdpath+ "/atguigu/" +filename;
//4). Create an output stream
FileOutputStream fos = new FileOutputStream (filePath);
4. Write Data
Fos.write (content.getbytes ("Utf-8"));
Fos.close ();
//5. Hint
Toast.maketext (this, "save Complete", 0). Show ();
} else {
Toast.maketext (this, "SD card is not mounted", 0). Show ();
}
}
public void Read2 (View v) throws Exception {
1. Determine the status of the SD card, if the status is mounted to continue, otherwise prompt
if (Environment.getexternalstoragestate (). Equals (
environment.media_mounted)) {
2. Read the file name of the input
String fileName = Et_of_name.gettext (). toString ();
3. Get the InputStream of the specified file
String Sdpath = Environment.getexternalstoragedirectory (). GetAbsolutePath ();
String FilePath = sdpath+ "/atguigu/" +filename;
FileInputStream fis = new FileInputStream (FilePath);
4. reading data into a string
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
byte[] buffer = new byte[1024];
int len =-1;
while ((Len=fis.read (buffer))!=-1) {
Baos.write (buffer, 0, Len);
}
String content = baos.tostring ();
Fis.close ();
5. Show
Et_of_content.settext (content);
} else {
Toast.maketext (This, "SD card is not mounted", 0). Show ();
}
}
}
Phone external file storage (SD card storage)