对手机SD卡的一些操作

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

代码如下:

 package com.car273.util;

 import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream; import org.apache.log4j.Logger; import android.os.Environment;
import android.os.StatFs; /**
* 关于SD卡上的操作方法集
*
* @author Administrator
*
*/
public class SDcardUtil {
/** log4j对象 */
private static final Logger logger = Logger.getLogger(SDcardUtil.class); /**
* 判断SD卡是否可用
*
* @return 有sd卡返回true,无sd卡返回false
*/
public static boolean isSDcardCanUse() {
try {
return Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);
} catch (Exception e) {
e.printStackTrace();
}
return false;
} /** 获取SD卡路径 **/
public static String getSDcardPath() {
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
if (sdCardExist) {
sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
}
if(sdDir ==null){
return "/mnt/sdcard";
}
return sdDir.toString();
} /**
* 外部存储空间信息
*
* @return 当前可用的存储空间大小
*/
public static long getExternalStorageSize() {
if (isSDcardCanUse()) {
// 判断外部存储空间
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
availableBlocks *= blockSize;
if (availableBlocks >= GlobalData.MIN_SPACE_SIZE)
return availableBlocks;
}
return 0;
} /**
* 获得文件大小
*
* @return 文件大小 -1表示文件不存在
* @throws FileNotFoundException
* @throws IOException
*/
@SuppressWarnings("resource")
public static int getFileSize(File file) throws FileNotFoundException,
IOException {
if (isFileExisted(file))
return (new FileInputStream(file)).available();
else
return -1;
} /**
* 判断文件是否存在
*
* @return
*/
public static boolean isFileExisted(File file) {
return file.exists();
} /**
* @param bytes
* 字节数组
* @param flag
* 是否追加形式写入文件
* @param dirPath
* 文件目录(目录不存在则创建目录)
* @param fileName
* 文件名
* @return 1:成功 0:写入过程中失败 -1:流关闭失败
* @throws IOException
*/
public static int writeSDCardByBytes(byte[] bytes, boolean flag,
String dirPath, String fileName) throws IOException {
OutputStream output = null;
try {
createDirInSDCard(dirPath);
output = new FileOutputStream(new File(dirPath + File.separator
+ fileName), flag);
output.write(bytes);
output.flush();
} catch (IOException e) {
e.printStackTrace();
logger.error(Car273Util.dumpThrowable(e));
throw e;
} finally {
try {
if (output != null)
output.close();
} catch (IOException e) {
logger.error(Car273Util.dumpThrowable(e));
e.printStackTrace();
return -1;
}
}
return 1;
} /**
* 根据路径dir创建文件目录
*
* @param dir
* @return
*/
private static File createDirInSDCard(String dir) {
File fileDir = new File(dir);
fileDir.mkdir();
return fileDir;
} /**
* 读取文件
* @param f 要读取的文件
* @return 文件信息字符串
* @throws IOException 读取异常
*/
public static String loadFileToString(File f) throws IOException {
// long beginTime = System.currentTimeMillis();
BufferedReader br = null;
String ret = null;
try {
br = new BufferedReader(new FileReader(f));
String line = null;
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null) {
sb.append(line);
}
ret = sb.toString();
} finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
}
}
}
return ret;
} }
上一篇:Codeforces Round #372 +#373 部分题解


下一篇:python CSS