对文件封装util
来源:互联网 发布:女人狼牙套用后感 知乎 编辑:程序博客网 时间:2024/05/18 00:53
package com.tools.util;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;public class FileUtil { public static void writeObject2File(Object obj, String path) { File file = new File(path); writeObject2File(obj, file); } /** * 写 object * @param obj * @param file */ public static void writeObject2File(Object obj, File file) { FileOutputStream out; try { out = new FileOutputStream(file); ObjectOutputStream objOut = new ObjectOutputStream(out); objOut.writeObject(obj); objOut.flush(); objOut.close(); } catch (IOException e) { e.printStackTrace(); } } public static Object readObjectFromFile(String path) { File file = new File(path); return readObjectFromFile(file); } /** * 读取保存的object * @param file * @return */ public static Object readObjectFromFile(File file) { Object temp = null; FileInputStream in; try { in = new FileInputStream(file); ObjectInputStream objIn = new ObjectInputStream(in); temp = objIn.readObject(); objIn.close(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return temp; } /** * 文件copy * @param oldPath * @param newPath */ public static void copy(String oldPath, String newPath) { FileInputStream fis = null; FileOutputStream fos = null; int byteread = 0; File oldfile = new File(oldPath); try { if (oldfile.exists()) { fis = new FileInputStream(oldPath); fos = new FileOutputStream(newPath); byte[] buffer = new byte[1024]; while ((byteread = fis.read(buffer)) != -1) { fos.write(buffer, 0, byteread); } } } catch (Exception e) { e.printStackTrace(); } finally { try { if (fis != null) { fis.close(); } if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } }public static File getCacheDir(Context ctx) { File file = ctx.getExternalCacheDir(); if (file == null) { file = ctx.getCacheDir(); } return file;}public static File getImgFileDir(Context ctx) { File file = ctx.getExternalFilesDir(Environment.DIRECTORY_PICTURES); if (file == null) { file = ctx.getFilesDir(); } return file;}public static File getTempDir() { File file = Environment.getExternalStorageDirectory(); File tempDir = new File(file.getAbsoluteFile() + "/iasku/temp/"); if (!tempDir.exists()) { //可以创建当前的file 没有上级目录的 也可以创建 tempDir.mkdirs(); } return tempDir;}public static File getDownloadDir() { String path = Environment.getExternalStorageDirectory().getAbsoluteFile() + "/iasku/downloads/"; File file = new File(path); if (!file.exists()) { file.mkdirs(); } return file;}/** * 音频文件保存路径 * @return */public static File getDownloadAudioDir() { String path = Environment.getExternalStorageDirectory().getAbsoluteFile() + "/iasku/audio/"; File file = new File(path); if (!file.exists()) { file.mkdirs(); } return file;}}
0 0
- 对文件封装util
- 对时间的封装util
- Lists是google对java.util.List类的封装
- java.util.zip对zip文件解压
- Mysql对文件操作的封装
- C#.net对XML文件类封装:
- mysql数据库对文件操作的封装
- 使用py2exe对Python文件进行封装生成exe文件
- 关于jdbc-util的封装
- SharedPreferences util工具类封装
- 封装一个对磁盘,文件夹,文件进行遍历的类
- Spring学习-Spring中对资源文件的封装
- 快速掌握 Mysql数据库对文件操作的封装
- action中实现对批量文件上传的封装
- 尝试用库文件对程序进行封装
- Java对.properties文件(即java.util.Properties类)的一些操作
- util.js文件介绍
- Java 文件 Util
- 基于nginx+HLS的流媒体服务器搭建(Ubuntu 14.04系统)
- 文件的简单读取,FileInputStream类的简单使用
- $.inArray函数用法
- Ubuntu12.04+QtCreator2.3.0+Qt4.8.1+opencv2.4.9(一)
- STM32F303X单片机USB例程详细解析3
- 对文件封装util
- 打印Http相应头字段的方法
- activity 生命周期演示
- linux每日学习(二)额外补充
- 蓝桥杯基础练习 十进制转换16进制
- 导航栏渐变效果
- 二叉树中根结点到某节点的路径(求某结点的所有祖先)
- 算法【5】:简单选择排序
- sdk manager更新