比较常用的文件处理类
2012-10-27 11:09
253 查看
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 block e.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(); } } } }
相关文章推荐
- 比较常用的文件处理类
- Shark和jbpm的配置文件处理方式比较
- Linux 上的常用文件传输方式介绍与比较
- Linux常用命令(4)之文件处理命令
- Linux 上的常用文件传输方式介绍与比较
- Linux 上的常用文件传输方式介绍与比较
- 人生的抉择—aspx、ashx、asmx文件处理请求效率比较
- Java常用文件目录处理代码集
- Linux 上的常用文件传输方式介绍与比较
- java常用类——java常用文件处理方法
- C常用字符串和文件处理函数
- linux下使用find xargs grep查找文件及文件内容,sed,gawk常用处理
- asp 中常用的文件处理函数
- Linux学习笔记(3)Linux常用命令之文件处理命令
- python3 常用模块(时间、os路径及文件操作、字符串比较,文件内容比较等模块)
- Linux常用命令(一)之文件处理命令
- Linux 上的常用文件传输方式介绍与比较
- 请帮助我写个批处理 比较两个文件的修改时间 (转载)
- PHP处理CSV表格文件的常用操作方法总结