package com.richerpay.ryshop.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
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.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.richerpay.ryshop.log.KLog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Environment;
import android.os.StatFs;
/**
* æä»¶å·¥å
·ç±»
*
* @version 1.0
*/
public class ToolFile {
private static final String TAG = ToolFile.class.getSimpleName();
/**
* æ£æ¥æ¯å¦å·²æè½½SDå¡éåï¼æ¯å¦åå¨SDå¡ï¼
*
* @return
*/
public static boolean isMountedSDCard() {
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
return true;
} else {
KLog.w(TAG, "SDCARD is not MOUNTED !");
return false;
}
}
/**
* è·åSDå¡å©ä½å®¹éï¼åä½Byteï¼
*
* @return
*/
@SuppressWarnings("deprecation")
public static long gainSDFreeSize() {
if (isMountedSDCard()) {
// åå¾SDå¡æä»¶è·¯å¾
File path = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(path.getPath());
// è·ååä¸ªæ°æ®åç大å°(Byte)
long blockSize = sf.getBlockSize();
// 空é²çæ°æ®åçæ°é
long freeBlocks = sf.getAvailableBlocks();
// è¿åSDå¡ç©ºé²å¤§å°
return freeBlocks * blockSize; // åä½Byte
} else {
return 0;
}
}
/**
* è·åSD塿»å®¹éï¼åä½Byteï¼
*
* @return
*/
@SuppressWarnings("deprecation")
public static long gainSDAllSize() {
if (isMountedSDCard()) {
// åå¾SDå¡æä»¶è·¯å¾
File path = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(path.getPath());
// è·ååä¸ªæ°æ®åç大å°(Byte)
long blockSize = sf.getBlockSize();
// è·åæææ°æ®åæ°
long allBlocks = sf.getBlockCount();
// è¿åSDå¡å¤§å°ï¼Byteï¼
return allBlocks * blockSize;
} else {
return 0;
}
}
/**
* è·åå¯ç¨çSDå¡è·¯å¾ï¼è¥SDå¡ä¸æ²¡ææè½½åè¿å""ï¼
*
* @return
*/
public static String gainSDCardPath() {
if (isMountedSDCard()) {
File sdcardDir = Environment.getExternalStorageDirectory();
if (!sdcardDir.canWrite()) {
KLog.w(TAG, "SDCARD can not write !");
}
return sdcardDir.getPath();
}
return "";
}
/**
* 以è¡ä¸ºåä½è¯»åæä»¶å
容ï¼ä¸æ¬¡è¯»ä¸æ´è¡ï¼å¸¸ç¨äºè¯»é¢åè¡çæ ¼å¼åæä»¶
*
* @param filePath
* æä»¶è·¯å¾
*/
public static String readFileByLines(String filePath) throws IOException {
BufferedReader reader = null;
StringBuffer sb = new StringBuffer();
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(filePath),
System.getProperty("file.encoding")));
String tempString = null;
while ((tempString = reader.readLine()) != null) {
sb.append(tempString);
sb.append("\n");
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
}
return sb.toString();
}
/**
* 以è¡ä¸ºåä½è¯»åæä»¶å
容ï¼ä¸æ¬¡è¯»ä¸æ´è¡ï¼å¸¸ç¨äºè¯»é¢åè¡çæ ¼å¼åæä»¶
*
* @param filePath
* æä»¶è·¯å¾
* @param encoding
* åæä»¶ç¼ç
*/
public static String readFileByLines(String filePath, String encoding)
throws IOException {
BufferedReader reader = null;
StringBuffer sb = new StringBuffer();
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(filePath), encoding));
String tempString = null;
while ((tempString = reader.readLine()) != null) {
sb.append(tempString);
sb.append("\n");
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
}
return sb.toString();
}
/**
* ä¿åå
容
*
* @param filePath
* æä»¶è·¯å¾
* @param content
* ä¿åçå
容
* @throws IOException
*/
public static void saveToFile(String filePath, String content)
throws IOException {
saveToFile(filePath, content, System.getProperty("file.encoding"));
}
/**
* æå®ç¼ç ä¿åå
容
*
* @param filePath
* æä»¶è·¯å¾
* @param content
* ä¿åçå
容
* @param encoding
* åæä»¶ç¼ç
* @throws IOException
*/
public static void saveToFile(String filePath, String content,
String encoding) throws IOException {
BufferedWriter writer = null;
File file = new File(filePath);
try {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file, false), encoding));
writer.write(content);
} finally {
if (writer != null) {
writer.close();
}
}
}
/**
* è¿½å ææ¬
*
* @param content
* éè¦è¿½å çå
容
* @param file
* å¾
è¿½å æä»¶æº
* @throws IOException
*/
public static void appendToFile(String content, File file)
throws IOException {
appendToFile(content, file, System.getProperty("file.encoding"));
}
/**
* è¿½å ææ¬
*
* @param content
* éè¦è¿½å çå
容
* @param file
* å¾
è¿½å æä»¶æº
* @param encoding
* æä»¶ç¼ç
* @throws IOException
*/
public static void appendToFile(String content, File file, String encoding)
throws IOException {
BufferedWriter writer = null;
try {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file, true), encoding));
writer.write(content);
} finally {
if (writer != null) {
writer.close();
}
}
}
/**
* 夿æä»¶æ¯å¦åå¨
*
* @param filePath
* æä»¶è·¯å¾
* @return æ¯å¦åå¨
* @throws Exception
*/
public static Boolean isExsit(String filePath) {
Boolean flag = false;
try {
File file = new File(filePath);
if (file.exists()) {
flag = true;
}
} catch (Exception e) {
KLog.e("夿æä»¶å¤±è´¥-->" + e.getMessage());
}
return flag;
}
/**
* å¿«é读åç¨åºåºç¨å
ä¸çæä»¶å
容
*
* @param context
* ä¸ä¸æ
* @param filename
* æä»¶åç§°
* @return æä»¶å
容
* @throws IOException
*/
public static String read(Context context, String filename)
throws IOException {
FileInputStream inStream = context.openFileInput(filename);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
return new String(data);
}
/**
* 读åæå®ç®å½æä»¶çæä»¶å
容
*
* @param fileName
* æä»¶åç§°
* @return æä»¶å
容
* @throws Exception
*/
@SuppressWarnings("resource")
public static String read(String fileName) throws IOException {
FileInputStream inStream = new FileInputStream(fileName);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
return new String(data);
}
/***
* 以è¡ä¸ºåä½è¯»åæä»¶å
容ï¼ä¸æ¬¡è¯»ä¸æ´è¡ï¼å¸¸ç¨äºè¯»é¢åè¡çæ ¼å¼åæä»¶
*
* @param fileName
* æä»¶åç§°
* @param encoding
* æä»¶ç¼ç
* @return å符串å
容
* @throws IOException
*/
public static String read(String fileName, String encoding)
throws IOException {
BufferedReader reader = null;
StringBuffer sb = new StringBuffer();
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(fileName), encoding));
String tempString = null;
while ((tempString = reader.readLine()) != null) {
sb.append(tempString);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
}
return sb.toString();
}
/**
* 读årawç®å½çæä»¶å
容
*
* @param context
* å
容ä¸ä¸æ
* @param rawFileId
* rawæä»¶åid
* @return
*/
public static String readRawValue(Context context, int rawFileId) {
String result = "";
try {
InputStream is = context.getResources().openRawResource(rawFileId);
int len = is.available();
byte[] buffer = new byte[len];
is.read(buffer);
result = new String(buffer, "UTF-8");
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 读åassetsç®å½çæä»¶å
容
*
* @param context
* å
容ä¸ä¸æ
* @param fileName
* æä»¶åç§°ï¼å
嫿©å±å
* @return
*/
public static String readAssetsValue(Context context, String fileName) {
String result = "";
try {
InputStream is = context.getResources().getAssets().open(fileName);
int len = is.available();
byte[] buffer = new byte[len];
is.read(buffer);
result = new String(buffer, "UTF-8");
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 读åassetsç®å½çæä»¶å
容
*
* @param context
* å
容ä¸ä¸æ
* @param fileName
* æä»¶åç§°ï¼å
嫿©å±å
* @return
*/
public static List