The reading and writing of SD cards is the most common operation in the development of Android applications. The following describes the SD card read and write operation mode:
1. Get the root directory of SD card
Copy Code code as follows:
String sdcardroot = Environment.getexternalstoragedirectory (). GetAbsolutePath ();
2. Create a folder directory on the SD card
/**
* Create directory on SD card/
public
File createdironsdcard (String dir)
{
file Dirfile = new file (Sdcardroot + File.separator + dir +file.separator);
LOG.V ("Createdironsdcard", Sdcardroot + file.separator + dir +file.separator);
Dirfile.mkdirs ();
return dirfile;
}
3. Create a file on the SD card
/**
* Create file on SD card *
/Public file
Createfileonsdcard (String fileName, String dir) throws IOException
{
File File = new file (sdcardroot + file.separator + dir + file.separator + fileName);
LOG.V ("Createfileonsdcard", Sdcardroot + file.separator + dir + file.separator + fileName);
File.createnewfile ();
return file;
}
4. Determine if the file exists in a directory on the SD card
/**
* To determine whether the file on the SD card exists
/public boolean isfileexist (string fileName, string path)
{file
file = New File (sdcardroot + path + file.separator + fileName);
return file.exists ();
}
5. Write data to SD card specify directory file
/* Write data to SD card
/Public File Writedata2sdcard (string path, string fileName, InputStream data)
{
File FILE = null;
OutputStream output = null;
try {
Createdironsdcard (path);//Create directory file
= Createfileonsdcard (fileName, path);//create file
output = new FileOutputStream (file);
byte buffer[] = new byte[2*1024]; 2K data per write
int temp;
while (temp = data.read (buffer))!=-1)
{
output.write (buffer,0,temp);
}
Output.flush ();
} catch (Exception e) {
e.printstacktrace ();
}
finally{
try {
output.close (); Turn off data flow operations
} catch (Exception E2) {
e2.printstacktrace ();
}
}
return file;
}
Attention matters
For the operation of the SD card, you must request permission:
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
See here for details http://www.jb51.net/article/34296.htm
Note: Do not read directly is to prevent the file operation on the memory consumption, so use the input stream read to the hard disk.
public string ReadFile (String fileName) throws exception{
FileInputStream fis = context.openfileinput (filename);
byte[] bytes = new byte[fis.available ()];
Fis.read (bytes);
Fis.close ();
return new String (bytes, "Utf-8");
}
When files are large, byte[] takes up a lot of memory.
Package cn.itcast.fileio.service;
Import Java.io.ByteArrayOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Android.content.Context;
Import android.os.Environment;
public class Sdcardservice {private Context ctx;
Public Sdcardservice (Context ctx) {this.ctx = CTX; /** * Write file into Skcard/public void Writetosdcard (string fileName, String cont) {try {//judge whether there is a Mount SDCA
Rd if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {//Get Sdcar file directory
File dir = environment.getexternalstoragedirectory ();
File File = new file (dir, "Itcast.txt");
FileOutputStream fos = new FileOutputStream (file);
Fos.write (Cont.getbytes ());
Fos.close ();
} catch (Exception e) {e.printstacktrace ();
}/** * Read the files in SDcard */public string Readsdcard (string fileName) {try {///Determine if there is a Mount SDcard if (Environment.getexternalstorag Estate (). Equals (environment.media_mounted)) {//Get sdcar file directory file Dir = environment.getexternal
Storagedirectory ();
File File = new file (dir, "Itcast.txt");
FileInputStream fis = new FileInputStream (file);
Return readis2string (FIS);
} catch (Exception e) {e.printstacktrace ();
return null; /** * Reads input stream data into the output stream/private OutputStream Readis2os (InputStream is, OutputStream os) {try {b
yte[] bytes = new byte[1024];
int length = 0;
while (length = is.read (bytes))!=-1) {os.write (bytes, 0, length);
} is.close ();
Os.close ();
catch (IOException e) {e.printstacktrace ();
} return OS; /** * Reads input stream data into the output stream/public byte[] Readis2bytes (InputStream is) {Bytearrayoutputstream =New Bytearrayoutputstream ();
Readis2os (Is,baos);
return Baos.tobytearray ();
Public String readis2string (InputStream are) {try {return a new String (Readis2bytes (IS), "Utf-8");
catch (Exception e) {e.printstacktrace ();
return null; public string readis2string (string fileName) {try {if environment.getexternalstoragestate (). Equals (Envi Ronment.
media_mounted)) {File dir = environment.getexternalstoragedirectory ();
File File = new file (dir,filename);
FileInputStream is = new FileInputStream (file);
Return readis2string (IS);
} catch (Exception e) {e.printstacktrace ();
return null;
}
}