zoukankan      html  css  js  c++  java
  • 对手机SD卡的一些操作

    首先要导入外包 log4j-1.2.16.jar

    代码如下:

      1 package com.car273.util;
      2 
      3 
      4 
      5 import java.io.BufferedReader;
      6 import java.io.File;
      7 import java.io.FileInputStream;
      8 import java.io.FileNotFoundException;
      9 import java.io.FileOutputStream;
     10 import java.io.FileReader;
     11 import java.io.IOException;
     12 import java.io.OutputStream;
     13 
     14 import org.apache.log4j.Logger;
     15 
     16 import android.os.Environment;
     17 import android.os.StatFs;
     18  20 
     21 /**
     22  * 关于SD卡上的操作方法集
     23  * 
     24  * @author Administrator
     25  * 
     26  */
     27 public class SDcardUtil {
     28     /** log4j对象 */
     29     private static final Logger logger = Logger.getLogger(SDcardUtil.class);
     30 
     31     /**
     32      * 判断SD卡是否可用
     33      * 
     34      * @return 有sd卡返回true,无sd卡返回false
     35      */
     36     public static boolean isSDcardCanUse() {
     37         try {
     38             return Environment.getExternalStorageState().equals(
     39                     Environment.MEDIA_MOUNTED);
     40         } catch (Exception e) {
     41             e.printStackTrace();
     42         }
     43         return false;
     44     }
     45 
     46     /** 获取SD卡路径 **/
     47     public static String getSDcardPath() {
     48         File sdDir = null;
     49         boolean sdCardExist = Environment.getExternalStorageState().equals(
     50                 android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
     51         if (sdCardExist) {
     52             sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
     53         }
     54         if(sdDir ==null){
     55             return "/mnt/sdcard";
     56         }
     57         return sdDir.toString();
     58     }
     59 
     60     /**
     61      * 外部存储空间信息
     62      * 
     63      * @return 当前可用的存储空间大小
     64      */
     65     public static long getExternalStorageSize() {
     66         if (isSDcardCanUse()) {
     67             // 判断外部存储空间
     68             File path = Environment.getExternalStorageDirectory();
     69             StatFs stat = new StatFs(path.getPath());
     70             long blockSize = stat.getBlockSize();
     71             long availableBlocks = stat.getAvailableBlocks();
     72             availableBlocks *= blockSize;
     73             if (availableBlocks >= GlobalData.MIN_SPACE_SIZE)
     74                 return availableBlocks;
     75         }
     76         return 0;
     77     }
     78 
     79     /**
     80      * 获得文件大小
     81      * 
     82      * @return 文件大小 -1表示文件不存在
     83      * @throws FileNotFoundException
     84      * @throws IOException
     85      */
     86     @SuppressWarnings("resource")
     87     public static int getFileSize(File file) throws FileNotFoundException,
     88             IOException {
     89         if (isFileExisted(file))
     90             return (new FileInputStream(file)).available();
     91         else
     92             return -1;
     93     }
     94 
     95     /**
     96      * 判断文件是否存在
     97      * 
     98      * @return
     99      */
    100     public static boolean isFileExisted(File file) {
    101         return file.exists();
    102     }
    103 
    104     /**
    105      * @param bytes
    106      *            字节数组
    107      * @param flag
    108      *            是否追加形式写入文件
    109      * @param dirPath
    110      *            文件目录(目录不存在则创建目录)
    111      * @param fileName
    112      *            文件名
    113      * @return 1:成功 0:写入过程中失败 -1:流关闭失败
    114      * @throws IOException
    115      */
    116     public static int writeSDCardByBytes(byte[] bytes, boolean flag,
    117             String dirPath, String fileName) throws IOException {
    118         OutputStream output = null;
    119         try {
    120             createDirInSDCard(dirPath);
    121             output = new FileOutputStream(new File(dirPath + File.separator
    122                     + fileName), flag);
    123             output.write(bytes);
    124             output.flush();
    125         } catch (IOException e) {
    126             e.printStackTrace();
    127             logger.error(Car273Util.dumpThrowable(e));
    128             throw e;
    129         } finally {
    130             try {
    131                 if (output != null)
    132                     output.close();
    133             } catch (IOException e) {
    134                 logger.error(Car273Util.dumpThrowable(e));
    135                 e.printStackTrace();
    136                 return -1;
    137             }
    138         }
    139         return 1;
    140     }
    141 
    142     /**
    143      * 根据路径dir创建文件目录
    144      * 
    145      * @param dir
    146      * @return
    147      */
    148     private static File createDirInSDCard(String dir) {
    149         File fileDir = new File(dir);
    150         fileDir.mkdir();
    151         return fileDir;
    152     }
    153     
    154     /**
    155      * 读取文件
    156      * @param f 要读取的文件
    157      * @return 文件信息字符串
    158      * @throws IOException 读取异常
    159      */
    160     public static String loadFileToString(File f) throws IOException {
    161         // long beginTime = System.currentTimeMillis();
    162         BufferedReader br = null;
    163         String ret = null;
    164         try {
    165             br = new BufferedReader(new FileReader(f));
    166             String line = null;
    167             StringBuffer sb = new StringBuffer();
    168             while ((line = br.readLine()) != null) {
    169                 sb.append(line);
    170             }
    171             ret = sb.toString();
    172         } finally {
    173             if (br != null) {
    174                 try {
    175                     br.close();
    176                 } catch (Exception e) {
    177                 }
    178             }
    179         }
    180         return ret;
    181     }
    182     
    183     
    184 }
  • 相关阅读:
    BZOJ4008: [HNOI2015]亚瑟王
    BZOJ4260: Codechef REBXOR
    BZOJ4408: [Fj Winter Camp 2016]神秘数
    BZOJ4010: [HNOI2015]菜肴制作
    ccc2016
    BZOJ3884: 上帝与集合的正确用法
    BZOJ1017: [JSOI2008]魔兽地图DotR
    BZOJ1011: [HNOI2008]遥远的行星
    BestCoder Round #73
    hdu4035(概率dp)
  • 原文地址:https://www.cnblogs.com/ws5861/p/3642166.html
Copyright © 2011-2022 走看看