package com.ymerp.android.tools;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.LineNumberReader;
import java.io.OutputStream;
import java.io.Reader;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
/**
* 文件操作工具
*
* @author chen.lin
*
*/
public class FileUtil {
private static final String TAG = "FileUtil";
/**
* 从sd卡取文件
*
* @param filename
* @return
*/
public String getFileFromSdcard(String filename) {
ByteArrayOutputStream outputStream = null;
FileInputStream fis = null;
try {
outputStream = new ByteArrayOutputStream();
File file = new File(Environment.getExternalStorageDirectory(), filename);
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
fis = new FileInputStream(file);
int len = 0;
byte[] data = new byte[1024];
while ((len = fis.read(data)) != -1) {
outputStream.write(data, 0, len);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
fis.close();
} catch (IOException e) {
}
}
return new String(outputStream.toByteArray());
}
/**
* 保存文件到sd
*
* @param filename
* @param content
* @return
*/
public static boolean saveContentToSdcard(String filename, String content) {
boolean flag = false;
FileOutputStream fos = null;
try {
File file = new File(Environment.getExternalStorageDirectory(), filename);
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
fos = new FileOutputStream(file);
fos.write(content.getBytes());
flag = true;
}
} catch (Exception e) {
e.printStackTrace();
flag = false;
} finally {
try {
fos.close();
} catch (IOException e) {
}
}
return flag;
}
/**
* 取得文件大小
*
* @param f
* @return
* @throws Exception
*/
@SuppressWarnings("resource")
public static long getFileSizes(File f) throws Exception {
long size = 0;
if (f.exists()) {
FileInputStream fis = null;
fis = new FileInputStream(f);
size = fis.available();
} else {
f.createNewFile();
}
return size;
}
/**
* 递归取得文件夹大小
*
* @param dir
* @return
* @throws Exception
*/
public static long getFileSize(File dir) throws Exception {
long size = 0;
File flist[] = dir.listFiles();
for (int i = 0; i < flist.length; i++) {
if (flist[i].isDirectory()) {
size = size + getFileSize(flist[i]);
} else {
size = size + flist[i].length();
}
}
return size;
}
/**
* 转换文件大小
*
* @param fileS
* @return
*/
public static String FormetFileSize(long fileS) {
DecimalFormat df = new DecimalFormat("#.00");
String fileSizeString = "";
if (fileS < 1024) {
fileSizeString = df.format((double) fileS) + "B";
} else if (fileS < 1048576) {
fileSizeString = df.format((double) fileS / 1024) + "K";
} else if (fileS < 1073741824) {
fileSizeString = df.format((double) fileS / 1048576) + "M";
} else {
fileSizeString = df.format((double) fileS / 1073741824) + "G";
}
return fileSizeString;
}
/**
* 递归求取目录文件个数
*
* @param f
* @return
*/
public static long getlist(File f) {
long size = 0;
File flist[] = f.listFiles();
size = flist.length;
for (int i = 0; i < flist.length; i++) {
if (flist[i].isDirectory()) {
size = size + getlist(flist[i]);
size--;
}
}
return size;
}
/**
* 在根目录下搜索文件
*
* @param keyword
* @return
*/
public static String searchFile(String keyword) {
String result = "";
File[] files = new File("/").listFiles();
for (File file : files) {
if (file.getName().indexOf(keyword) >= 0) {
result += file.getPath() + "\n";
}
}
if (result.equals("")) {
result = "找不到文件!!";
}
return result;
}
/**
* @detail 搜索sdcard文件
* @param 需要进行文件搜索的目录
* @param 过滤搜索文件类型
* */
public static List search(File file, String[] ext) {
List list = new ArrayList();
if (file != null) {
if (file.isDirectory()) {
File[] listFile = file.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
search(listFile[i], ext);
}
}
} else {
String filename = file.getAbsolutePath();
for (int i = 0; i < ext.length; i++) {
if (filename.endsWith(ext[i])) {
list.add(filename);
break;
}
}
}
}
return list;
}
/**
* 查询文件
*
* @param file
* @param keyword
* @return
*/
public static List FindFile(File file, String keyword) {
List list = new ArrayList();
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null) {
for (File tempf : files) {
if (tempf.isDirectory()) {
if (tempf.getName().toLowerCase().lastIndexOf(keyword) > -1) {
list.add(tempf);
}
list.addAll(FindFile(tempf, keyword));
} else {
if (tempf.getName().toLowerCase().lastIndexOf(keyword) > -1) {
list.add(tempf);
}
}
}
}
}
return list;
}
/**
* searchFile 查找文件并加入到ArrayList 当中去
*
* @param context
* @param keyword
* @param filepath
* @return
*/
public static List
———————————————————————
有需求者请加qq:136137465,非诚勿扰
(java 架构师全套教程,共760G, 让你从零到架构师,每月轻松拿3万)
01.高级架构师四十二个阶段高
02.Java高级系统培训架构课程148课时
03.Java高级互联网架构师课程
04.Java互联网架构Netty、Nio、Mina等-视频教程
05.Java高级架构设计2016整理-视频教程
06.架构师基础、高级片
07.Java架构师必修linux运维系列课程
08.Java高级系统培训架构课程116课时
(送:hadoop系列教程,java设计模式与数据结构, Spring Cloud微服务, SpringBoot入门)
——————————————————————–