Android之文件读写工具类
来源:互联网 发布:eclipse软件使用教程 编辑:程序博客网 时间:2024/06/05 20:55
本工具类永久维护,永久更新,如果各位读者发现有bug或者不合理之处,欢迎指正,博主将第一时间改正。
以下是主要内容,本类主要功能有:
1.创建文件功能;
2.向文件中写入字节数组;
3.向文件中写入字符串;
4.从文件中读取字节数组;
5.从文件中读取字符串;
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;/** * 文件读写工具类 * * @author bear * */public class FileUtil {/** * 如果文件不存在,就创建文件 * * @param path 文件路径 * @return */public static String createIfNotExist(String path) {File file = new File(path);if (!file.exists()) {try {file.createNewFile();} catch (Exception e) {System.out.println(e.getMessage());}}return path;}/** * 向文件中写入数据 * * @param filePath * 目标文件全路径 * @param data * 要写入的数据 * @return true表示写入成功 false表示写入失败 */public static boolean writeBytes(String filePath, byte[] data) {try {FileOutputStream fos = new FileOutputStream(filePath);fos.write(data);fos.close();return true;} catch (Exception e) {System.out.println(e.getMessage());}return false;}/** * 从文件中读取数据 * * @param file * @return */public static byte[] readBytes(String file) {try {FileInputStream fis = new FileInputStream(file);int len = fis.available();byte[] buffer = new byte[len];fis.read(buffer);fis.close();return buffer;} catch (Exception e) {System.out.println(e.getMessage());}return null;}/** * 向文件中写入字符串String类型的内容 * * @param file * 文件路径 * @param content * 文件内容 * @param charset * 写入时候所使用的字符集 */public static void writeString(String file, String content, String charset) {try {byte[] data = content.getBytes(charset);writeBytes(file, data);} catch (Exception e) {System.out.println(e.getMessage());}}/** * 从文件中读取数据,返回类型是字符串String类型 * * @param file * 文件路径 * @param charset * 读取文件时使用的字符集,如utf-8、GBK等 * @return */public static String readString(String file, String charset) {byte[] data = readBytes(file);String ret = null;try {ret = new String(data, charset);} catch (Exception e) {System.out.println(e.getMessage());}return ret;}}
1 0
- Android之文件读写工具类
- Android 文件读写工具类
- android 文件读写工具类
- 读写文件工具类
- Android之文件读写
- Java 读写文件工具类
- Excel文件读写工具类
- java 文件读写工具类
- 属性文件读写工具类
- Unity3D工具类 - 读写文件
- 文件读写通用工具类
- Android开发之读写文件
- Android游戏之文件读写类和绘图类设计
- Android 封装工具类之文件缓存
- PropertyUtil属性文件读写工具类
- 简单的属性文件读写工具类
- c#文件读写数据工具类
- 文件读写操作工具类整理
- C++精确计算代码执行时间精度微秒级
- Java中的嵌套类、内部类、静态内部类
- Maximum Product Subarray -- leetcode
- 黑马程序员——基础知识总结_接口,多态
- 求职的途径
- Android之文件读写工具类
- Mat函数的定义与vector存储到Mat中的问题
- Unity中 Plugin 跨语言 类型转换
- 链式栈的接口实现
- C++ 虚析构函数 分析
- unity开发:Qt C++与unity之间UDP网络通信
- 四个服务器设计模型
- iOS实现tableView下拉搜索功能
- 推荐系统——SVD/SVD++