package com.martin.simpledevelop.utils.file; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.Comparator; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.http.Header; import org.apache.http.HttpResponse; import android.content.Context; import android.content.pm.PackageInfo; import android.content.res.AssetManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.Drawable; import android.os.Environment; import android.os.StatFs; import com.martin.simpledevelop.utils.app.SaAppUtils; import com.martin.simpledevelop.utils.global.SaAppConfig; import com.martin.simpledevelop.utils.image.SaImageUtils; import com.martin.simpledevelop.utils.log.SaLogUtils; import com.martin.simpledevelop.utils.security.SaMD5Utils; import com.martin.simpledevelop.utils.string.SaStrUtils; /** * @Description 文件的操作工具类 * @File SaFileUtils.java * @Package com.martin.simpledevelop.utils.file * @Date 2015年6月26日上午1:09:42 * @Author Donghongyu 1358506549@qq.com * @Version v1.0.0 */ @SuppressWarnings("deprecation") public class SaFileUtils { /** * Log 输出标签 */ public static String TAG = SaFileUtils.class.getName(); /** * 默认APP根目录. */ private static String downloadRootDir = null; /** * 默认下载图片文件目录. */ private static String imageDownloadDir = null; /** * 默认下载文件目录. */ private static String fileDownloadDir = null; /** * 默认缓存目录. */ private static String cacheDownloadDir = null; /** * 默认下载数据库文件的目录. */ private static String dbDownloadDir = null; /** * 剩余空间大于200M才使用SD缓存. */ private static int freeSdSpaceNeededToCache = 200 * 1024 * 1024; /** * 描述:通过文件的网络地址从SD卡中读取图片,如果SD中没有则自动下载并保存. * * @param url 文件的网络地址 * @param type 图片的处理类型(剪切或者缩放到指定大小,参考ImageUtil类) 如果设置为原图,则后边参数无效,得到原图 * @param desiredWidth 新图片的宽 * @param desiredHeight 新图片的高 * @return Bitmap 新图片 */ public static Bitmap getBitmapFromSD(String url, int type, int desiredWidth, int desiredHeight) { Bitmap bitmap = null; try { if (SaStrUtils.isEmpty(url)) { return null; } // SD卡不存在 或者剩余空间不足了就不缓存到SD卡了 if (!isCanUseSD() || freeSpaceOnSD() < freeSdSpaceNeededToCache) { bitmap = getBitmapFromURL(url, type, desiredWidth, desiredHeight); return bitmap; } // 下载文件,如果不存在就下载,存在直接返回地址 String downFilePath = downloadFile(url); if (downFilePath != null) { SaLogUtils.d(TAG, "下载文件,如果不存在就下载,存在直接返回地址."); // 获取图片 return getBitmapFromSD(new File(downFilePath), type, desiredWidth, desiredHeight); } else { return null; } } catch (Exception e) { e.printStackTrace(); SaLogUtils.e(TAG, "通过文件的网络地址从SD卡中读取图片,如果SD中没有则自动下载并保异常:" + e.getMessage()); } return bitmap; } /** * 描述:通过文件的本地地址从SD卡读取图片. * * @param file the file * @param type 图片的处理类型(剪切或者缩放到指定大小,参考ImageUtil类) 如果设置为原图,则后边参数无效,得到原图 * @param desiredWidth 新图片的宽 * @param desiredHeight 新图片的高 * @return Bitmap 新图片 */ public static Bitmap getBitmapFromSD(File file, int type, int desiredWidth, int desiredHeight) { Bitmap bitmap = null; try { // SD卡是否存在 if (!isCanUseSD()) { return null; } // 文件是否存在 if (!file.exists()) { return null; } // 文件存在 if (type == SaImageUtils.CUTIMG) { bitmap = SaImageUtils.cutImg(file, desiredWidth, desiredHeight); SaLogUtils.d(TAG, "要下载的文件存在-->直接返回地址-->裁剪文件"); } else if (type == SaImageUtils.SCALEIMG) { bitmap = SaImageUtils.scaleImg(file, desiredWidth, desiredHeight); SaLogUtils.d(TAG, "要下载的文件存在-->直接返回地址-->缩放文件"); } else { bitmap = SaImageUtils.getBitmap(file); SaLogUtils.d(TAG, "要下载的文件存在-->直接返回地址-->原文件"); } } catch (Exception e) { e.printStackTrace(); SaLogUtils.e(TAG, "通过文件的本地地址从SD卡读取图片异常:" + e.getMessage()); } return bitmap; } /** * 描述:通过文件的本地地址从SD卡读取图片. * * @param file the file * @return Bitmap 图片 */ public static Bitmap getBitmapFromSD(File file) { Bitmap bitmap = null; try { // SD卡是否存在 if (!isCanUseSD()) { return null; } // 文件是否存在 if (!file.exists()) { SaLogUtils.d(TAG, "通过文件的本地地址从SD卡读取图片,文件是不存在."); return null; } // 文件存在 bitmap = SaImageUtils.getBitmap(file); SaLogUtils.d(TAG, "通过文件的本地地址从SD卡读取图片,文件存在."); } catch (Exception e) { e.printStackTrace(); SaLogUtils.e(TAG, "通过文件的本地地址从SD卡读取图片异常:" + e.getMessage()); } return bitmap; } /** * 描述:将图片的byte[]写入本地文件. * * @param imgByte 图片的byte[]形势 * @param fileName 文件名称,需要包含后缀,如.jpg * @param type 图片的处理类型(剪切或者缩放到指定大小,参考ImageUtil类) * @param desiredWidth 新图片的宽 * @param desiredHeight 新图片的高 * @return Bitmap 新图片 */ public static Bitmap getBitmapFromByte(byte[] imgByte, String fileName, int type, int desiredWidth, int desiredHeight) { FileOutputStream fos = null; DataInputStream dis = null; ByteArrayInputStream bis = null; Bitmap bitmap = null; File file = null; try { if (imgByte != null) { file = new File(imageDownloadDir, fileName); if (!file.exists()) { file.createNewFile(); } fos = new FileOutputStream(file); int readLength = 0; bis = new ByteArrayInputStream(imgByte); dis = new DataInputStream(bis); byte[] buffer = new byte[1024]; while ((readLength = dis.read(buffer)) != -1) { fos.write(buffer, 0, readLength); try { Thread.sleep(500); } catch (Exception e) { } } fos.flush(); bitmap = getBitmapFromSD(file, type, desiredWidth, desiredHeight); SaLogUtils.d(TAG, "将图片的byte[]写入本地文件-->" + bitmap); } } catch (Exception e) { e.printStackTrace(); } finally { if (dis != null) { try { dis.close(); } catch (Exception e) { } } if (bis != null) { try { bis.close(); } catch (Exception e) { } } if (fos != null) { try { fos.close(); } catch (Exception e) { } } } return bitmap; } /** * 描述:根据URL从互连网获取图片. * * @param url 要下载文件的网络地址 * @param type 图片的处理类型(剪切或者缩放到指定大小,参考ImageUtil类) * @param desiredWidth 新图片的宽 * @param desiredHeight 新图片的高 * @return Bitmap 新图片 */ public static Bitmap getBitmapFromURL(String url, int type, int desiredWidth, int desiredHeight) { Bitmap bit = null; try { bit = SaImageUtils .getBitmap(url, type, desiredWidth, desiredHeight); SaLogUtils.d(TAG, "根据URL从互连网获取图片成功-->" + bit); } catch (Exception e) { e.printStackTrace(); SaLogUtils.e(TAG, "下载图片异常:" + e.getMessage()); } return bit; } /** * 描述:获取src中的图片资源. * * @param src 图片的src路径,如(“image/arrow.png”) * @return Bitmap 图片 */ public static Bitmap getBitmapFromSrc(String src) { Bitmap bit = null; try { bit = BitmapFactory.decodeStream(SaFileUtils.class .getResourceAsStream(src)); SaLogUtils.d(TAG, "获取src中的图片资源成功-->" + bit); } catch (Exception e) { e.printStackTrace(); SaLogUtils.e(TAG, "获取图片异常:" + e.getMessage()); } return bit; } /** * 描述:获取Asset中的图片资源.Bitmap * * @param context the context * @param fileName the file name * @return Bitmap 图片 */ public static Bitmap getBitmapFromAsset(Context context, String fileName) { Bitmap bit = null; try { AssetManager assetManager = context.getAssets(); InputStream is = assetManager.open(fileName); bit = BitmapFactory.decodeStream(is); SaLogUtils.d(TAG, "获取Asset中的图片资源成功.Bitmap-->" + bit); } catch (Exception e) { e.printStackTrace(); SaLogUtils.e(TAG, "获取图片异常:" + e.getMessage()); } return bit; } /** * 描述:获取Asset中的图片资源.Drawable * * @param context the context * @param fileName the file name * @return Drawable 图片 */ public static Drawable getDrawableFromAsset(Context context, String fileName) { Drawable drawable = null; try { AssetManager assetManager = context.getAssets(); InputStream is = assetManager.open(fileName); drawable = Drawable.createFromStream(is, null); SaLogUtils.d(TAG, "获取Asset中的图片资源成功.Drawable-->" + drawable); } catch (Exception e) { e.printStackTrace(); SaLogUtils.e(TAG, "获取图片异常:" + e.getMessage()); } return drawable; } /** * 下载网络文件到SD卡中.如果SD中存在同名文件将不再下载 * * @param url 要下载文件的网络地址 * @return 下载好的本地文件地址 */ public static String downloadFile(String url) { InputStream in = null; FileOutputStream fileOutputStream = null; HttpURLConnection connection = null; String downFilePath = null; File file = null; try { if (!isCanUseSD()) { return null; } // 先判断SD卡中有没有这个文件,不比较后缀部分比较 String fileNameNoMIME = getCacheFileNameFromUrl(url); File parentFile = new File(imageDownloadDir); File[] files = parentFile.listFiles(); for (int i = 0; i < files.length; ++i) { String fileName = files[i].getName(); String name = fileName.substring(0, fileName.lastIndexOf(".")); if (name.equals(fileNameNoMIME)) { // 文件已存在 SaLogUtils.d(TAG, "文件已存在-->" + files[i].getPath()); return files[i].getPath(); } } URL mUrl = new URL(url); connection = (HttpURLConnection) mUrl.openConnection(); connection.connect(); // 获取文件名,下载文件 String fileName = getCacheFileNameFromUrl(url, connection); file = new File(imageDownloadDir, fileName); downFilePath = file.getPath(); if (!file.exists()) { file.createNewFile(); } else { // 文件已存在 SaLogUtils.d(TAG, "文件已存在-->" + file.getPath()); return file.getPath(); } in = connection.getInputStream(); fileOutputStream = new FileOutputStream(file); byte[] b = new byte[1024]; int temp = 0; while ((temp = in.read(b)) != -1) { fileOutputStream.write(b, 0, temp); } } catch (Exception e) { e.printStackTrace(); SaLogUtils.d(TAG, "有文件下载出错了,已删除"); // 检查文件大小,如果文件为0B说明网络不好没有下载成功,要将建立的空文件删除 if (file != null) { file.delete(); } file = null; downFilePath = null; } finally { try { if (in != null) { in.close(); } } catch (Exception e) { e.printStackTrace(); } try { if (fileOutputStream != null) { fileOutputStream.close(); } } catch (Exception e) { e.printStackTrace(); } try { if (connection != null) { connection.disconnect(); } } catch (Exception e) { e.printStackTrace(); } } return downFilePath; } /** * 描述:获取网络文件的大小. * * @param Url 图片的网络路径 * @return int 网络文件的大小 */ public static int getContentLengthFromUrl(String Url) { int mContentLength = 0; try { URL url = new URL(Url); HttpURLConnection mHttpURLConnection = (HttpURLConnection) url .openConnection(); mHttpURLConnection.setConnectTimeout(5 * 1000); mHttpURLConnection.setRequestMethod("GET"); mHttpURLConnection .setRequestProperty( "Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); mHttpURLConnection.setRequestProperty("Accept-Language", "zh-CN"); mHttpURLConnection.setRequestProperty("Referer", Url); mHttpURLConnection.setRequestProperty("Charset", "UTF-8"); mHttpURLConnection .setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"); mHttpURLConnection.setRequestProperty("Connection", "Keep-Alive"); mHttpURLConnection.connect(); if (mHttpURLConnection.getResponseCode() == 200) { // 根据响应获取文件大小 mContentLength = mHttpURLConnection.getContentLength(); SaLogUtils.d(TAG, "根据响应获取文件大小-->" + mContentLength); } } catch (Exception e) { e.printStackTrace(); SaLogUtils.e(TAG, "获取长度异常:" + e.getMessage()); } return mContentLength; } /** * 获取文件名,通过网络获取. * * @param url 文件地址 * @return 文件名 */ public static String getRealFileNameFromUrl(String url) { String name = null; try { if (SaStrUtils.isEmpty(url)) { return name; } URL mUrl = new URL(url); HttpURLConnection mHttpURLConnection = (HttpURLConnection) mUrl .openConnection(); mHttpURLConnection.setConnectTimeout(5 * 1000); mHttpURLConnection.setRequestMethod("GET"); mHttpURLConnection .setRequestProperty( "Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); mHttpURLConnection.setRequestProperty("Accept-Language", "zh-CN"); mHttpURLConnection.setRequestProperty("Referer", url); mHttpURLConnection.setRequestProperty("Charset", "UTF-8"); mHttpURLConnection.setRequestProperty("User-Agent", ""); mHttpURLConnection.setRequestProperty("Connection", "Keep-Alive"); mHttpURLConnection.connect(); if (mHttpURLConnection.getResponseCode() == 200) { for (int i = 0; ; i++) { String mime = mHttpURLConnection.getHeaderField(i); if (mime == null) { break; } if ("content-disposition".equals(mHttpURLConnection .getHeaderFieldKey(i).toLowerCase())) { Matcher m = Pattern.compile(".*filename=(.*)").matcher( mime.toLowerCase()); if (m.find()) { SaLogUtils.d(TAG, "获取文件名,通过网络获取-->" + m.group(1).replace("\"", "")); return m.group(1).replace("\"", ""); } } } } } catch (Exception e) { e.printStackTrace(); SaLogUtils.e(TAG, "网络上获取文件名失败"); } return name; } /** * 获取真实文件名(xx.后缀),通过网络获取. * * @param connection 连接 * @return 文件名 */ public static String getRealFileName(HttpURLConnection connection) { String name = null; try { if (connection == null) { return name; } if (connection.getResponseCode() == 200) { for (int i = 0; ; i++) { String mime = connection.getHeaderField(i); if (mime == null) { break; } // "Content-Disposition","attachment; filename=1.txt" // Content-Length;就是当用户想把请求所得的内容存为一个文件的时候提供一个默认的文件名 if ("content-disposition".equals(connection .getHeaderFieldKey(i).toLowerCase())) { Matcher m = Pattern.compile(".*filename=(.*)").matcher( mime.toLowerCase()); if (m.find()) { SaLogUtils.d(TAG, "获取文件名,通过网络获取-->" + m.group(1).replace("\"", "")); return m.group(1).replace("\"", ""); } } } } } catch (Exception e) { e.printStackTrace(); SaLogUtils.e(TAG, "网络上获取文件名失败"); } return name; } /** * 获取真实文件名(xx.后缀),通过网络获取. * * @param response the response * @return 文件名 */ public static String getRealFileName(HttpResponse response) { String name = null; try { if (response == null) { return name; } // 获取文件名 Header[] headers = response.getHeaders("content-disposition"); for (int i = 0; i < headers.length; i++) { Matcher m = Pattern.compile(".*filename=(.*)").matcher( headers[i].getValue()); if (m.find()) { name = m.group(1).replace("\"", ""); SaLogUtils.d(TAG, "网络上获取文件名成功-->\r\n" + name); } } } catch (Exception e) { e.printStackTrace(); SaLogUtils.e(TAG, "网络上获取文件名失败"); } return name; } /** * 获取文件名(不含后缀).MD5加密之后的 * * @param url 文件地址 * @return 文件名 */ public static String getCacheFileNameFromUrl(String url) { if (SaStrUtils.isEmpty(url)) { return null; } String name = null; try { name = SaMD5Utils.encode(url); SaLogUtils.d(TAG, "网络上获取文件名成功,获取文件名(不含后缀)MD5加密后的.-->\r\n" + name); } catch (Exception e) { e.printStackTrace(); SaLogUtils.e(TAG, "网络上获取文件名失败"); } return name; } /** * 获取文件名(.后缀),外链模式和通过网络获取. * * @param url 文件地址 * @param response the response * @return 文件名 */ public static String getCacheFileNameFromUrl(String url, HttpResponse response) { if (SaStrUtils.isEmpty(url)) { return null; } String name = null; try { // 获取后缀 String suffix = getMIMEFromUrl(url, response); if (SaStrUtils.isEmpty(suffix)) { suffix = ".ab"; } name = SaMD5Utils.encode(url) + suffix; SaLogUtils.d(TAG, " 获取文件名(.后缀),外链模式和通过网络获取,MD5加密后的.-->\r\n" + name); } catch (Exception e) { e.printStackTrace(); SaLogUtils.e(TAG, "网络上获取文件名失败"); } return name; } /** * 获取文件名(.后缀),外链模式和通过网络获取. * * @param url 文件地址 * @param connection the connection * @return 文件名 */ public static String getCacheFileNameFromUrl(String url, HttpURLConnection connection) { if (SaStrUtils.isEmpty(url)) { return null; } String name = null; try { // 获取后缀 String suffix = getMIMEFromUrl(url, connection); if (SaStrUtils.isEmpty(suffix)) { suffix = ".ab"; } name = SaMD5Utils.encode(url) + suffix; SaLogUtils.d(TAG, " 获取文件名(.后缀),外链模式和通过网络获取,MD5加密后的.-->\r\n" + name); } catch (Exception e) { e.printStackTrace(); SaLogUtils.e(TAG, "网络上获取文件名失败"); } return name; } /** * 获取文件后缀,本地. * * @param url 文件地址 * @param connection the connection * @return 文件后缀 */ public static String getMIMEFromUrl(String url, HttpURLConnection connection) { if (SaStrUtils.isEmpty(url)) { return null; } String suffix = null; try { // 获取后缀 if (url.lastIndexOf(".") != -1) { suffix = url.substring(url.lastIndexOf(".")); if (suffix.indexOf("/") != -1 || suffix.indexOf("?") != -1 || suffix.indexOf("&") != -1) { suffix = null; } } if (SaStrUtils.isEmpty(suffix)) { // 获取文件名 这个效率不高 String fileName = getRealFileName(connection); if (fileName != null && fileName.lastIndexOf(".") != -1) { suffix = fileName.substring(fileName.lastIndexOf(".")); } } } catch (Exception e) { e.printStackTrace(); SaLogUtils.e(TAG, "本地.获取文件名失败"); } SaLogUtils.d(TAG, " 获取文件名(.后缀),外链模式和通过网络获取,MD5加密后的文件后缀.-->" + suffix); return suffix; } /** * 获取文件后缀,本地和网络. * * @param url 文件地址 * @param response the response * @return 文件后缀 */ public static String getMIMEFromUrl(String url, HttpResponse response) { if (SaStrUtils.isEmpty(url)) { return null; } String mime = null; try { // 获取后缀 if (url.lastIndexOf(".") != -1) { mime = url.substring(url.lastIndexOf(".")); if (mime.indexOf("/") != -1 || mime.indexOf("?") != -1 || mime.indexOf("&") != -1) { mime = null; } } if (SaStrUtils.isEmpty(mime)) { // 获取文件名 这个效率不高 String fileName = getRealFileName(response); if (fileName != null && fileName.lastIndexOf(".") != -1) { mime = fileName.substring(fileName.lastIndexOf(".")); } } } catch (Exception e) { e.printStackTrace(); SaLogUtils.e(TAG, "本地.获取文件名失败"); } SaLogUtils.d(TAG, "获取文件后缀,本地和网络-->" + mime); return mime; } /** * 描述:从sd卡中的文件读取到byte[]. * * @param path sd卡中文件路径 * @return byte[] */ public static byte[] getByteArrayFromSD(String path) { byte[] bytes = null; ByteArrayOutputStream out = null; try { File file = new File(path); // SD卡是否存在 if (!isCanUseSD()) { return null; } // 文件是否存在 if (!file.exists()) { return null; } long fileSize = file.length(); if (fileSize > Integer.MAX_VALUE) { return null; } FileInputStream in = new FileInputStream(path); out = new ByteArrayOutputStream(1024); byte[] buffer = new byte[1024]; int size = 0; while ((size = in.read(buffer)) != -1) { out.write(buffer, 0, size); } in.close(); bytes = out.toByteArray(); SaLogUtils.d(TAG, "从sd卡中的文件读取到byte[]成功-->" + bytes.length); } catch (Exception e) { e.printStackTrace(); SaLogUtils.e(TAG, "从sd卡中的文件读取到byte[]失败"); } finally { if (out != null) { try { out.close(); } catch (Exception e) { } } } return bytes; } /** * 描述:将byte数组写入文件. * * @param path the path * @param content the content * @param create the create */ public static void writeByteArrayToSD(String path, byte[] content, boolean create) { FileOutputStream fos = null; try { File file = new File(path); // SD卡是否存在 if (!isCanUseSD()) { return; } // 文件是否存在 if (!file.exists()) { if (create) { File parent = file.getParentFile(); if (!parent.exists()) { parent.mkdirs(); file.createNewFile(); } } else { return; } } fos = new FileOutputStream(path); fos.write(content); SaLogUtils.d(TAG, "将byte数组写入文件.成功-->" + fos); } catch (Exception e) { e.printStackTrace(); SaLogUtils.e(TAG, "将byte数组写入文件-->失败"); } finally { if (fos != null) { try { fos.close(); } catch (Exception e) { } } } } /** * 描述:SD卡是否能用. * * @return true 可用,false不可用 */ public static boolean isCanUseSD() { try { SaLogUtils.d( TAG, "SD卡是否能用-->" + Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)); return Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED); } catch (Exception e) { e.printStackTrace(); } SaLogUtils.d(TAG, "SD卡是否能用-->" + false); return false; } /** * 描述:初始化存储目录. * * @param context the context */ public static void initFileDir(Context context) { PackageInfo info = SaAppUtils.getPackageInfo(context); // 默认下载文件根目录. String downloadRootPath = File.separator + SaAppConfig.DOWNLOAD_ROOT_DIR + File.separator + info.packageName + File.separator; // 默认下载图片文件目录. String imageDownloadPath = downloadRootPath + SaAppConfig.DOWNLOAD_IMAGE_DIR + File.separator; // 默认下载文件目录. String fileDownloadPath = downloadRootPath + SaAppConfig.DOWNLOAD_FILE_DIR + File.separator; // 默认缓存目录. String cacheDownloadPath = downloadRootPath + SaAppConfig.CACHE_DIR + File.separator; // 默认DB目录. String dbDownloadPath = downloadRootPath + SaAppConfig.DB_DIR + File.separator; try { if (!isCanUseSD()) { return; } else { File root = Environment.getExternalStorageDirectory(); File downloadDir = new File(root.getAbsolutePath() + downloadRootPath); if (!downloadDir.exists()) { downloadDir.mkdirs(); } downloadRootDir = downloadDir.getPath(); File cacheDownloadDirFile = new File(root.getAbsolutePath() + cacheDownloadPath); if (!cacheDownloadDirFile.exists()) { cacheDownloadDirFile.mkdirs(); } cacheDownloadDir = cacheDownloadDirFile.getPath(); File imageDownloadDirFile = new File(root.getAbsolutePath() + imageDownloadPath); if (!imageDownloadDirFile.exists()) { imageDownloadDirFile.mkdirs(); } imageDownloadDir = imageDownloadDirFile.getPath(); File fileDownloadDirFile = new File(root.getAbsolutePath() + fileDownloadPath); if (!fileDownloadDirFile.exists()) { fileDownloadDirFile.mkdirs(); } fileDownloadDir = fileDownloadDirFile.getPath(); File dbDownloadDirFile = new File(root.getAbsolutePath() + dbDownloadPath); if (!dbDownloadDirFile.exists()) { dbDownloadDirFile.mkdirs(); } dbDownloadDir = dbDownloadDirFile.getPath(); } } catch (Exception e) { e.printStackTrace(); } } /** * 计算sdcard上的剩余空间. * * @return the int */ public static int freeSpaceOnSD() { StatFs stat = new StatFs(Environment.getExternalStorageDirectory() .getPath()); double sdFreeMB = ((double) stat.getAvailableBlocks() * (double) stat .getBlockSize()) / 1024 * 1024; SaLogUtils.d(TAG, "计算sdcard上的剩余空间-->" + sdFreeMB); return (int) sdFreeMB; } /** * 根据文件的最后修改时间进行排序. */ public static class FileLastModifSort implements Comparator<File> { public int compare(File arg0, File arg1) { if (arg0.lastModified() > arg1.lastModified()) { return 1; } else if (arg0.lastModified() == arg1.lastModified()) { return 0; } else { return -1; } } } /** * 删除所有缓存文件. * * @return true, if successful */ public static boolean clearDownloadFile() { try { if (!isCanUseSD()) { SaLogUtils.e(TAG, "SD卡不可用"); return false; } File fileDirectory = new File(downloadRootDir); deleteFile(fileDirectory); SaLogUtils.d(TAG, "删除所有缓存文件成功-->"); } catch (Exception e) { e.printStackTrace(); SaLogUtils.e(TAG, "删除所有缓存文件失败."); return false; } return true; } /** * 删除制定文件夹的中的便利删除里面的文件 * * @param file */ public static void deleteFile(File file) { if (file.exists()) { // 判断文件是否存在 if (file.isFile()) { // 判断是否是文件 file.delete(); // delete()方法 你应该知道 是删除的意思; } else if (file.isDirectory()) { // 否则如果它是一个目录 File files[] = file.listFiles(); // 声明目录下所有的文件 files[]; for (int i = 0; i < files.length; i++) { // 遍历目录下所有的文件 SaLogUtils.d(TAG, "删除缓存文件中-->" + files[i].getPath()); deleteFile(files[i]); // 把每个文件 用这个方法进行迭代 } } file.delete(); } else { SaLogUtils.e(TAG, "删除所有缓存文件失败.文件不存在"); } } /** * 描述:读取Assets目录的文件内容. * * @param context the context * @param name the name * @param encoding the encoding * @return the string */ public static String readAssetsByName(Context context, String name, String encoding) { String text = null; InputStreamReader inputReader = null; BufferedReader bufReader = null; try { inputReader = new InputStreamReader(context.getAssets().open(name)); bufReader = new BufferedReader(inputReader); String line = null; StringBuffer buffer = new StringBuffer(); while ((line = bufReader.readLine()) != null) { buffer.append(line); } text = new String(buffer.toString().getBytes(), encoding); SaLogUtils.d(TAG, "读取Assets目录的文件内容-->" + text); } catch (Exception e) { e.printStackTrace(); SaLogUtils.e(TAG, "读取Assets目录的文件内容失败"); } finally { try { if (bufReader != null) { bufReader.close(); } if (inputReader != null) { inputReader.close(); } } catch (Exception e) { e.printStackTrace(); } } return text; } /** * @param context 打开Asset下的文件 * @param fileName 文件名 * @return */ public static InputStream openAssetFile(Context context, String fileName) { AssetManager am = context.getAssets(); InputStream is = null; try { is = am.open(fileName); } catch (IOException e) { e.printStackTrace(); } SaLogUtils.i(TAG, "打开Asset下的文件:\r\n" + is); return is; } /** * 描述:读取Raw目录的文件内容. * * @param context the context * @param id the id * @param encoding the encoding * @return the string */ public static String readRawByName(Context context, int id, String encoding) { String text = null; InputStreamReader inputReader = null; BufferedReader bufReader = null; try { inputReader = new InputStreamReader(context.getResources() .openRawResource(id)); bufReader = new BufferedReader(inputReader); String line = null; StringBuffer buffer = new StringBuffer(); while ((line = bufReader.readLine()) != null) { buffer.append(line); } text = new String(buffer.toString().getBytes(), encoding); SaLogUtils.d(TAG, "读取Raw目录的文件内容-->" + text); } catch (Exception e) { e.printStackTrace(); SaLogUtils.e(TAG, "读取Raw目录的文件内容"); } finally { try { if (bufReader != null) { bufReader.close(); } if (inputReader != null) { inputReader.close(); } } catch (Exception e) { e.printStackTrace(); } } return text; } /** * 获取下载根目录。<br> * Gets the download root dir. * * @param context the context * @return the download root dir */ public static String getDownloadRootDir(Context context) { if (downloadRootDir == null) { initFileDir(context); } return downloadRootDir; } /** * 获取图像的下载目录。<br> * Gets the image download dir. * * @param context the context * @return the image download dir */ public static String getImageDownloadDir(Context context) { if (downloadRootDir == null) { initFileDir(context); } return imageDownloadDir; } /** * 获取文件的下载目录。<br> * Gets the file download dir. * * @param context the context * @return the file download dir */ public static String getFileDownloadDir(Context context) { if (downloadRootDir == null) { initFileDir(context); } return fileDownloadDir; } /** * 获取缓存下载目录。 <br> * Gets the cache download dir. * * @param context the context * @return the cache download dir */ public static String getCacheDownloadDir(Context context) { if (downloadRootDir == null) { initFileDir(context); } return cacheDownloadDir; } /** * 获取数据库的下载目录。<br> * Gets the db download dir. * * @param context the context * @return the db download dir */ public static String getDbDownloadDir(Context context) { if (downloadRootDir == null) { initFileDir(context); } return dbDownloadDir; } /** * 获取需要缓存的自由空间,SD。<br> * Gets the free sd space needed to cache. * * @return the free sd space needed to cache */ public static int getFreeSdSpaceNeededToCache() { return freeSdSpaceNeededToCache; } /** * 写入文件 * * @param in * @param file */ public static void write(InputStream in, File file) { if (file.exists()) { file.delete(); } try { file.createNewFile(); FileOutputStream out = new FileOutputStream(file); byte[] buffer = new byte[1024]; while (in.read(buffer) > -1) { out.write(buffer); } out.flush(); in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 写入文件 * * @param in * @param file */ public static void write(String in, File file, boolean append) { if (file.exists()) { file.delete(); } try { file.createNewFile(); FileWriter fw = new FileWriter(file, append); fw.write(in); fw.flush(); fw.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 读文件 * * @param file * @return */ public static String read(File file) { if (!file.exists()) { return ""; } try { FileReader reader = new FileReader(file); BufferedReader br = new BufferedReader(reader); StringBuffer buffer = new StringBuffer(); String s; while ((s = br.readLine()) != null) { buffer.append(s); } return buffer.toString(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; } }