- 检测网络是否可用
/**
* 检测网络是否可用
*
* @param context
* @return
*/
public static boolean isNetWorkConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
}
return false;
}
2.检测Sdcard是否存在
/**
* 检测Sdcard是否存在
*
* @return
*/
public static boolean isExitsSdcard() {
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
return true;
else
return false;
}
- 获取文件夹大小
/**
* 获取文件夹大小
*
* @param file
* File实例
* @return long
*/
public static long getFolderSize(File file) {
long size = 0;
if (file.exists()) {
try {
File[] fileList = file.listFiles();
for (int i = 0; i < fileList.length; i++) {
if (fileList[i].isDirectory()) {
size = size + getFolderSize(fileList[i]);
} else {
size = size + fileList[i].length();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return size;
}
4.删除指定目录下文件及目录
/**
* 删除指定目录下文件及目录
*
* @param deleteThisPath
* @param filepath
* @return
*/
public void deleteFolderFile(String filePath, boolean deleteThisPath) {
if (!TextUtils.isEmpty(filePath)) {
try {
File file = new File(filePath);
if (file.isDirectory()) {// 处理目录
File files[] = file.listFiles();
for (int i = 0; i < files.length; i++) {
deleteFolderFile(files[i].getAbsolutePath(), true);
}
}
if (deleteThisPath) {
if (!file.isDirectory()) {// 如果是文件,删除
file.delete();
} else {// 目录
if (file.listFiles().length == 0) {// 目录下没有文件或者目录,删除
file.delete();
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
5.对文件大小进行格式化
/**
* 格式化单位
*
* @param size
* @return
*/
public static String getFormatSize(double size) {
double kiloByte = size / 1024;
if (kiloByte < 1) {
return size + "B";
}
double megaByte = kiloByte / 1024;
if (megaByte < 1) {
BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
return result1.setScale(2, BigDecimal.ROUND_HALF_UP)
.toPlainString() + "KB";
}
double gigaByte = megaByte / 1024;
if (gigaByte < 1) {
BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
return result2.setScale(2, BigDecimal.ROUND_HALF_UP)
.toPlainString() + "MB";
}
double teraBytes = gigaByte / 1024;
if (teraBytes < 1) {
BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
return result3.setScale(2, BigDecimal.ROUND_HALF_UP)
.toPlainString() + "GB";
}
BigDecimal result4 = new BigDecimal(teraBytes);
return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()
+ "TB";
}
6.创建资源文件
/**
* 创建资源文件
* @param fileName
* @return
*/
public File createSDFile(String fileName){
File file=new File(fileName);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
7.进行写文件操作
/**
* 进行写文件操作
* @param fileName
* @param inputStream
* @return
*/
public File writeToSDfromInput(String fileName,InputStream inputStream){
//createSDDir(path);
File file=createSDFile(fileName);
OutputStream outStream=null;
try {
outStream=new FileOutputStream(file);
byte[] buffer = new byte[8192];
int count;
while ((count = inputStream.read(buffer))> 0){
outStream.write(buffer, 0, count);
}
outStream.close();
inputStream.close();
// outStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
outStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
8.进行写文件操作
/**
* 进行写文件操作
* @param fileName
* @param data
* @return
*/
public File operaFileData(String fileName, byte[] data) {
{
FileOutputStream fileout = null;
File file = new File(fileName);
if (file.exists()) {
file.delete();
}
try {
fileout = new FileOutputStream(file);
fileout.write(data, 0, data.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
}