Android实现读写SD卡

2019-12-10 18:02:27刘景俊

  5.将数据写入到SD卡指定目录文件

/* 写入数据到SD卡中 
   */ 
  public File writeData2SDCard(String path, String fileName, InputStream data) 
  { 
    File file = null; 
    OutputStream output = null; 
     
    try { 
      createDirOnSDCard(path); //创建目录 
      file = createFileOnSDCard(fileName, path); //创建文件 
      output = new FileOutputStream(file); 
      byte buffer[] = new byte[2*1024];     //每次写2K数据 
      int temp; 
      while((temp = data.read(buffer)) != -1 ) 
      { 
        output.write(buffer,0,temp); 
      } 
      output.flush(); 
       
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
    finally{ 
      try { 
        output.close();  //关闭数据流操作 
      } catch (Exception e2) { 
        e2.printStackTrace(); 
      } 
    } 
     
    return file; 
  } 

   注意事项

      对SD卡的操作,必须要申请权限:   

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

注意:不直接进行读出是为了防止打文件操作对内存的消耗,所以用输入流读到硬盘上。

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");
  }

当文件很大的时候,byte[]会占用很大的内存。

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;
  }
 
  /**
   * 写文件入skcard
   */
  public void writeToSdCard(String fileName, String cont) {
    try {
      // 判断是否有挂载sdcard
      if (Environment.getExternalStorageState().equals(
          Environment.MEDIA_MOUNTED)) {
        // 得到sdcar文件目录
        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();
    }
  }
  /**
   * 读SdCard中的文件
   */
  public String readSdCard(String fileName) {
    try {
      // 判断是否有挂载sdcard
      if (Environment.getExternalStorageState().equals(
          Environment.MEDIA_MOUNTED)) {
        // 得到sdcar文件目录
        File dir = Environment.getExternalStorageDirectory();
        File file = new File(dir, "itcast.txt");
        FileInputStream fis = new FileInputStream(file);
        return readIs2String(fis);
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
   
  /**
   * 将输入流数据读取到输出流当中
   */
  private OutputStream readIs2Os(InputStream is ,OutputStream os){
    try {
      byte[] 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 ;
  }
   
  /**
   * 将输入流数据读取到输出流当中
   */
  public byte[] readIs2Bytes(InputStream is){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    readIs2Os(is,baos);
    return baos.toByteArray() ;
  }
   
  public String readIs2String(InputStream is){
    try {
      return new String(readIs2Bytes(is),"utf-8");
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    return null ;
  }
   
  public String readIs2String(String fileName){
    try {
      if(Environment.getExternalStorageState().equals(Environment.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 ;
  }
}