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