Reading and writing SD cards is the most common operation we use in developing Android applications. The following is a description of the read and write operation methods of SD cards:
1. Obtain the root directory of the SD card
String sdCardRoot = ().getAbsolutePath();
2. Create a folder directory on the SD card
/** * Create a directory on the SD card */ public File createDirOnSDCard(String dir) { File dirFile = new File(sdCardRoot + + dir +); ("createDirOnSDCard", sdCardRoot + + dir +); (); return dirFile; }
3. Create a file on the SD card
/** * Create a file on the SD card */ public File createFileOnSDCard(String fileName, String dir) throws IOException { File file = new File(sdCardRoot + + dir + + fileName); ("createFileOnSDCard", sdCardRoot + + dir + + fileName); (); return file; }
4. Determine whether the file exists in a directory on the SD card
/** * Determine whether the file on the SD card exists */ public boolean isFileExist(String fileName, String path) { File file = new File(sdCardRoot + path + + fileName); return (); }
5. Write data to the specified directory file of the SD card
/* Write data to the SD card */ public File writeData2SDCard(String path, String fileName, InputStream data) { File file = null; OutputStream output = null; try { createDirOnSDCard(path); //Create a directory file = createFileOnSDCard(fileName, path); //Create a file output = new FileOutputStream(file); byte buffer[] = new byte[2*1024]; //Write 2K data each time int temp; while((temp = (buffer)) != -1 ) { (buffer,0,temp); } (); } catch (Exception e) { (); } finally{ try { (); //Close data flow operation } catch (Exception e2) { (); } } return file; }
Things to note
For the operation of SD card, you must apply for permission:
<uses-permission android:name=".WRITE_EXTERNAL_STORAGE"/>
See here for detailshttps:///article/
Note: Not reading directly is to prevent the memory consumption of file typing operations, so use the input stream to read it on the hard disk.
public String readFile(String fileName) throws Exception{ FileInputStream fis = (fileName); byte[] bytes = new byte[()]; (bytes); (); return new String(bytes,"utf-8"); }
When the file is large, byte[] will take up a lot of memory.
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class SdCardService { private Context ctx; public SdCardService(Context ctx) { = ctx; } /** * Write files into skcard */ public void writeToSdCard(String fileName, String cont) { try { // Determine whether sdcard is mounted if (().equals( Environment.MEDIA_MOUNTED)) { // Get the sdcar file directory File dir = (); File file = new File(dir, ""); FileOutputStream fos = new FileOutputStream(file); (()); (); } } catch (Exception e) { (); } } /** * Read files in SdCard */ public String readSdCard(String fileName) { try { // Determine whether sdcard is mounted if (().equals( Environment.MEDIA_MOUNTED)) { // Get the sdcar file directory File dir = (); File file = new File(dir, ""); FileInputStream fis = new FileInputStream(file); return readIs2String(fis); } } catch (Exception e) { (); } return null; } /** * Read the input stream data into the output stream */ private OutputStream readIs2Os(InputStream is ,OutputStream os){ try { byte[] bytes = new byte[1024]; int length = 0 ; while((length = (bytes)) != -1){ (bytes, 0, length); } (); (); } catch (IOException e) { (); } return os ; } /** * Read the input stream data into the output stream */ public byte[] readIs2Bytes(InputStream is){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); readIs2Os(is,baos); return () ; } public String readIs2String(InputStream is){ try { return new String(readIs2Bytes(is),"utf-8"); } catch (Exception e) { (); } return null ; } public String readIs2String(String fileName){ try { if(().equals(Environment.MEDIA_MOUNTED)){ File dir = (); File file = new File(dir,fileName); FileInputStream is = new FileInputStream(file); return readIs2String(is); } } catch (Exception e) { (); } return null ; } }