文件操作工具类和字符串判断是否为空
2016-12-28 21:09
302 查看
import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.media.MediaScannerConnection; import android.os.Environment; import android.support.annotation.Nullable; import java.io.BufferedReader; import java.io.BufferedWriter; 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.OutputStream; import java.nio.channels.FileChannel; /** * Created by yanchengmeng on 16/12/9. */ public class FileUtil { /** * 保存图片到本地 * * @param bitmap * @param path * @param fileName * @return 保存的路径,如果为null表示保存失败 */ public static String saveBitMap(Bitmap bitmap, String path, String fileName) { if (bitmap == null || path == null || fileName == null) { return null; } File storePath = new File(path); String result = saveBitMap(bitmap, storePath, fileName); return result; } /** * 保存图片到本地 * * @param bitmap * @param path * @param fileName * @return */ public static String saveBitMap(Bitmap bitmap, File path, String fileName) { if (bitmap == null || path == null || fileName == null) { return null; } if (!path.exists()) { if (!path.mkdirs()) { return null; } } File destFile = new File(path, fileName); String result = destFile.getAbsolutePath(); OutputStream os = null; try { os = new FileOutputStream(destFile); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os); os.flush(); os.close(); } catch (IOException e) { result = null; } finally { if (os != null) { try { os.close(); } catch (IOException e) { } } } return result; } /** * 刷新图片到相册中 * * @param context * @param path */ public static void mediaRefresh(Activity context, File path) { MediaScannerConnection.scanFile(context, new String[]{path.getAbsolutePath()}, null, null); } /** * 刷新图片到相册中 * * @param context * @param path */ public static void mediaRefresh(Activity context, String path) { MediaScannerConnection.scanFile(context, new String[]{path}, null, null); } /** * 从指定文件中读取String * * @param fileName * @return */ public static String readFromFile(File fileName) { StringBuilder sb = new StringBuilder(); BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(fileName)); String tempString; while ((tempString = reader.readLine()) != null) { sb.append(tempString); } } catch (IOException e) { } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { } } return sb.toString(); } /** * 把String写入指定文件 * * @param fileName * @param text */ public static void writeToFile(File fileName, String text) { BufferedWriter writer = null; try { writer = new BufferedWriter(new FileWriter(fileName)); writer.write(text); writer.flush(); } catch (IOException e) { } finally { try { if (writer != null) { writer.close(); } } catch (IOException e) { } } } /** * 复制单个文件 * * @param oldPath String 原文件路径 如:c:/fqf.txt * @param newPath String 复制后路径 如:f:/fqf.txt * @return boolean */ public static void copyFile(String oldPath, String newPath) { try { int bytesum = 0; int byteread = 0; File oldfile = new File(oldPath); if (oldfile.exists()) { //文件存在时 InputStream inStream = new FileInputStream(oldPath); //读入原文件 FileOutputStream fs = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; int length; while ((byteread = inStream.read(buffer)) != -1) { bytesum += byteread; //字节数 文件大小 System.out.println(bytesum); fs.write(buffer, 0, byteread); } inStream.close(); } } catch (Exception e) { System.out.println("复制单个文件操作出错"); e.printStackTrace(); } } /** * 使用文件通道的方式复制文件 * * @param s 源文件 * @param t 复制到的新文件 */ public static void copyFile(File s, File t) { FileInputStream fi = null; FileOutputStream fo = null; FileChannel in = null; FileChannel out = null; try { fi = new FileInputStream(s); fo = new FileOutputStream(t); in = fi.getChannel();//得到对应的文件通道 out = fo.getChannel();//得到对应的文件通道 in.transferTo(0, in.size(), out);//连接两个通道,并且从in通道读取,然后写入out通道 } catch (IOException e) { e.printStackTrace(); } finally { try { fi.close(); in.close(); fo.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } } public static String getCacheDir(Context context) { String cacheDir = null; try { if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable()) { cacheDir = context.getExternalCacheDir().getPath(); if (StringUtil.isEmpty(cacheDir)) { cacheDir = context.getCacheDir().getPath(); } } else { cacheDir = context.getCacheDir().getPath(); } } catch (Exception e) { } if (StringUtil.isEmpty(cacheDir)){ return context.getCacheDir().getPath(); } return cacheDir; } @Nullable public static File getExternalCacheDir(Context context) { try { return context.getExternalFilesDir(null); } catch (Exception e){ return null; } } public static boolean saveBitmap(Bitmap bm, String picName) { try { File f = new File(picName); if (f.exists()) { f.delete(); } FileOutputStream out = new FileOutputStream(f); bm.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); return true; } catch (FileNotFoundException e) { return false; } catch (IOException e) { e.printStackTrace(); return false; } } public static boolean fileExist(String path, String fileName) { File file = new File(path); if (!file.exists()) { return false; } if (!file.isDirectory()) { return false; } String[] tempList = file.list(); for (int i = 0; i < tempList.length; i++) { if (fileName.equals(tempList[i])) { return true; } } return false; } 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]);//再删除空文件夹 } } } //删除文件夹 public static void delFolder(String folderPath) { try { delAllFile(folderPath); //删除完里面所有内容 String filePath = folderPath; filePath = filePath.toString(); File myFilePath = new File(filePath); myFilePath.delete(); //删除空文件夹 } catch (Exception e) { e.printStackTrace(); } } public static boolean isEmpty(String str) { if (null == str || str.trim().equals("") || str.trim().equalsIgnoreCase("null")) { return true; } return false; } }
相关文章推荐
- android 字符串工具类 格式化日期字符串 拼接数组 判断字符串是否为空 转换时间显示 转换文件大小 获取中英文字符个数
- C# 判断 检测 access 建库 建表 文件是否存在等 数据操作 使用 ADOX
- 判断量文件相等,判断某文本中是否含有某字符串
- C#路径的八种相关操作,判断字符串是否为路径等
- 将字符串添加到指定的文件中去 AppendAllText ;WriteAllText ;----------------判断指定路径的文件是否存在File.Exists(Path)
- Android获取SD卡上的APK文件,并且判断是否安装过、是否可以更新到新版本的工具类
- 操作 Wave 文件(2): 判断一个文件是否是 Wave 文件
- 类库------对一些字符串进行操作的类1.判断输入是否数字2.截取字符串函数3.过滤输入信息4.生成随机数5.生成验证码图片6.获取汉字第一个拼音7.半角转全角8.全角转半角
- 在Shell里面判断字符串是否为空 ,文件存在,目录存在
- c# 文件操作 判断文件是否为空的方法
- 文件操作函数feof判断是否指向文件的结尾处,结尾即为空字符
- 监测程序详细介绍,快捷键的设置;txt文件里面一行行的读取、删除、添加字符串等;根据程序路径名以及程序名判断程序是否在运行;
- Android获取SD卡上的APK文件,并且判断是否安装过、是否可以更新到新版本的工具类
- 判断字符串是否由数字组成 自动生成6位的密码 数据库操作,定位记录返回其值,或者返回默认值
- Iphone文件操作和字符串操作的工具类
- 工具类,文件及字符串操作的常用
- Unix C中的一些文件操作(判断是否存在,文件打开,删除等)
- C语言文件操作 fopen, fclose, mkdir(打开关闭文件,建文件夹,判断文件是否存在可读或可写)
- 9-18 ASP第二讲实现数据的真实操作,从txt文件中读取进行校对,判断登陆是否成功
- ASP.NET对文件的操作,创建文件,判断文件是否存在,判断文件是否存在删除文件夹