android sd卡文件读取

17 篇文章 0 订阅

 

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import android.graphics.Bitmap;
import android.os.Environment;

import com.ihandy.local.core.exception.Logger;
import com.ihandy.sxtbybuat.ui.resource.ImageResource;

public class SDCardUtil {

 private static final String TAG = null;
 
 private static String SD_CARD_PATH;

 public static byte[] readFile(String filename) throws IOException {
  BufferedInputStream bufferedInputStream = null;
  try {
   bufferedInputStream = new BufferedInputStream(new FileInputStream(filename));
   int len = bufferedInputStream.available();
   byte[] bytes = new byte[len];
   int r = bufferedInputStream.read(bytes);
   if (len != r) {
    bytes = null;
    throw new IOException("读取文件不正确");
   }
   return bytes;
  } finally {
   close(bufferedInputStream);
  }
 }

 public static String readFileToString(String fileFullPath) throws Exception {
  StringBuffer sbf = new StringBuffer();
  BufferedReader reader = null;
  try {
   reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileFullPath), "GBK"));
   String lineContent = null;
   // 一次读入一行,直到读入null为文件结束
   while ((lineContent = reader.readLine()) != null) {
    sbf.append(lineContent);
    sbf.append("\n");
   }
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   if (reader != null) {
    try {
     reader.close();
    } catch (IOException e1) {
    }
   }
  }
  return sbf.toString();
 }

 /**
  * 按照规定名保存一个文件
  *
  * @param userFace
  * @param userNO
  * @return
  */
 public static String savePhotoToSDCard(Bitmap userFace, String userNO) {
  if(userFace!=null) {
   File savedir = new File(getAvatarSavedDir());
   if (!savedir.exists()) {
    savedir.mkdirs();
   }
   File photoFile = new File(savedir.getPath() + userNO + ".png");
   FileOutputStream fOut = null;
   try {
    photoFile.createNewFile();
    fOut = new FileOutputStream(photoFile);
    userFace.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    fOut.flush();
    return photoFile.getAbsolutePath();
   } catch (IOException e) {
    Logger.printStackTrace(TAG, e);
   } finally {
    close(fOut);
   } 
  }
  return "";
 }

 

 /**
  * 取得SD卡的路径
  *
  * @return
  */
 public static String getSDCardPath() {
  if (!StringUtils.isEmpty(SD_CARD_PATH)) {
   return SD_CARD_PATH;
  }
  File sdDir = null;
  boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
  if (sdCardExist) {
   sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
   SD_CARD_PATH = sdDir.getPath();
   return SD_CARD_PATH;
  }
  return "";
 }
 
 /*** 营销员头像保存的目录
  * @return
  */
 public static String getAvatarSavedDir(){
  return SDCardUtil.getSDCardPath() + ImageResource.SD_CPIC;
 }

 /**
  * 关闭文件
  *
  * @param c
  */
 private static void close(Closeable c) {
  if (c != null) {
   try {
    c.close();
   } catch (IOException e) {
    // do nothing.
   }
  }
 }

 /**
  * 判断指定目录下是否存在某文件
  *
  * @param path
  * @return
  */
 public static boolean isSDCardExistFile(String filePath) {
  File filedir = new File(filePath);
  if (filedir.exists()) {
   return true;
  }
  return false;
 }
 
 
  /**
     * 递归删除 文件夹的所有文件,并删除该文件夹
     ** @param directory  要删除的文件夹
     */
    public static void deleteDirectory(File directory){
        if (directory==null || !directory.exists()) {
            return;
        }
       
        try {
         cleanDirectory(directory);
        }catch (IOException e) {
  }
     directory.delete();
    }
   
    /**
     * Cleans a directory without deleting it.
     * @param directory directory to clean
     * @throws IOException in case cleaning is unsuccessful
     */
    private static void cleanDirectory(File directory) throws IOException {
        if (!directory.exists()) {
            String message = directory + " does not exist";
            throw new IllegalArgumentException(message);
        }

        if (!directory.isDirectory()) {
            String message = directory + " is not a directory";
            throw new IllegalArgumentException(message);
        }

        File[] files = directory.listFiles();
        if (files == null) {  // null if security restricted
            throw new IOException("Failed to list contents of " + directory);
        }

        IOException exception = null;
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            try {
                forceDelete(file);
            } catch (IOException ioe) {
                exception = ioe;
            }
        }

        if (null != exception) {
            throw exception;
        }
    }
   
   
    public static void forceDelete(File file) throws IOException {
        if (file.isDirectory()) {
            deleteDirectory(file);
        } else {
            boolean filePresent = file.exists();
            if (!file.delete()) {
                if (!filePresent){
                    throw new FileNotFoundException("File does not exist: " + file);
                }
                String message =
                    "Unable to delete file: " + file;
                throw new IOException(message);
            }
        }
    }
   
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值