比较常用的文件处理类

来源:互联网 发布:阿里云和百度云 编辑:程序博客网 时间:2024/05/21 10:48
package com.aijia.util;/** * 一些文件处理 */import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.util.Log;import android.widget.ImageView;public class FileUtil {/** * 显示图片的缩略图 *  * @param uri  图片路径 * @param size图片比例 * @return */public static Bitmap getOpBitmap(String uri, int size) {BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;Bitmap bitmap = BitmapFactory.decodeFile(uri, options);options.inJustDecodeBounds = false;int be = (int) (options.outHeight / (float) size);if (be <= 0)be = 1;options.inSampleSize = be;options.inPreferredConfig = Bitmap.Config.RGB_565;File file = new File(uri);InputStream in = null;try {in = new FileInputStream(file);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}if (in != null) {bitmap = BitmapFactory.decodeStream(in, null, options);try {in.close();} catch (IOException e) {e.printStackTrace();}}// bitmap = BitmapFactory.decodeFile(uri, options);return bitmap;}/** * 删除文件夹 *  * @param folderPath */public static void delFolder(String folderPath) {try {delAllFile(folderPath); // 删除完里面所有内容String filePath = folderPath;filePath = filePath.toString();java.io.File myFilePath = new java.io.File(filePath);myFilePath.delete(); // 删除空文件夹} catch (Exception e) {System.out.println("删除文件夹操作出错");e.printStackTrace();}}/** * 删除所有文件 * @param path */public static void delAllFile(String path) {File file = new File(path);if (!file.exists()) {return;}if (!file.isDirectory()) {return;}String[] tempList = file.list();File temp = null;for (int i = 0; i < tempList.length; i++) {if (path.endsWith(File.separator)) {temp = new File(path + tempList[i]);} else {temp = new File(path + File.separator + tempList[i]);}if (temp.isFile()) {temp.delete();}if (temp.isDirectory()) {delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件delFolder(path + "/" + tempList[i]);// 再删除空文件夹}}}/** * 判断指定路徑的文件是否存在 *  * @param path * @return */public static boolean existFile(String path) {File file = new File(path);if (file.exists()) {return true;} else {return false;}}/** * 如果给定路径不存在这个文件夹,创建文件夹 *  * @param path */public static void createFolder(String path) {File file = new File(path);if (!file.exists()) {file.mkdirs();}}/** * 如果給定路徑存在這個文件,刪除這個文件 * @param path */public static void deleteFile(String path) {File file = new File(path);if (file.exists()) {file.delete();}}/** * 下载图片到本地 *  * @param urlPath * @param savePath * @throws Exception */public static void downloadImg(String urlPath, String savePath)throws Exception {// 获取图片if (!existFile(savePath)) {//如果不存在这个文件String folderpath = savePath.substring(0, savePath.lastIndexOf("/"));createFolder(folderpath);//创建相应的文件夹String str = java.net.URLEncoder.encode(urlPath, "UTF-8");str = str.replaceAll("%2F", "/");// 需要把网址的特殊字符转过来str = str.replaceAll("%3A", ":");URL url = new URL(str);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout(3 * 1000);if (conn.getResponseCode() == 200) {InputStream inSream = conn.getInputStream();File file = new File(savePath);FileOutputStream outStream = new FileOutputStream(file);byte[] buffer = new byte[1024];int len = -1;while ((len = inSream.read(buffer)) != -1) {outStream.write(buffer, 0, len);}outStream.close();inSream.close();}}}}


原创粉丝点击