[Android工具类]Android File工具类

本文介绍了一个Android平台上的文件读写工具类,包括创建文件、写入与读取字节数组及字符串等功能,提供了实用的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Android之文件读写工具类


整理的Android工具类,封装文件读写操作

1.创建文件功能;

2.向文件中写入字节数组;

3.向文件中写入字符串;

4.从文件中读取字节数组;

5.从文件中读取字符串;


[java]  view plain  copy
  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileOutputStream;  
  4.   
  5. /** 
  6.  * 文件读写工具类 
  7.  *  
  8.  * @author bear 
  9.  * 
  10.  */  
  11. public class FileUtil {  
  12.   
  13.     /** 
  14.      * 如果文件不存在,就创建文件 
  15.      *  
  16.      * @param path 文件路径 
  17.      * @return 
  18.      */  
  19.     public static String createIfNotExist(String path) {  
  20.         File file = new File(path);  
  21.         if (!file.exists()) {  
  22.             try {  
  23.                 file.createNewFile();  
  24.             } catch (Exception e) {  
  25.                 System.out.println(e.getMessage());  
  26.             }  
  27.         }  
  28.         return path;  
  29.     }  
  30.   
  31.     /** 
  32.      * 向文件中写入数据 
  33.      *  
  34.      * @param filePath 
  35.      *            目标文件全路径 
  36.      * @param data 
  37.      *            要写入的数据 
  38.      * @return true表示写入成功  false表示写入失败 
  39.      */  
  40.     public static boolean writeBytes(String filePath, byte[] data) {  
  41.         try {  
  42.             FileOutputStream fos = new FileOutputStream(filePath);  
  43.             fos.write(data);  
  44.             fos.close();  
  45.             return true;  
  46.         } catch (Exception e) {  
  47.             System.out.println(e.getMessage());  
  48.         }  
  49.         return false;  
  50.     }  
  51.   
  52.     /** 
  53.      * 从文件中读取数据 
  54.      *  
  55.      * @param file 
  56.      * @return 
  57.      */  
  58.     public static byte[] readBytes(String file) {  
  59.         try {  
  60.             FileInputStream fis = new FileInputStream(file);  
  61.             int len = fis.available();  
  62.             byte[] buffer = new byte[len];  
  63.             fis.read(buffer);  
  64.             fis.close();  
  65.             return buffer;  
  66.         } catch (Exception e) {  
  67.             System.out.println(e.getMessage());  
  68.         }  
  69.   
  70.         return null;  
  71.   
  72.     }  
  73.   
  74.     /** 
  75.      * 向文件中写入字符串String类型的内容 
  76.      *  
  77.      * @param file 
  78.      *            文件路径 
  79.      * @param content 
  80.      *            文件内容 
  81.      * @param charset 
  82.      *            写入时候所使用的字符集 
  83.      */  
  84.     public static void writeString(String file, String content, String charset) {  
  85.         try {  
  86.             byte[] data = content.getBytes(charset);  
  87.             writeBytes(file, data);  
  88.         } catch (Exception e) {  
  89.             System.out.println(e.getMessage());  
  90.         }  
  91.   
  92.     }  
  93.   
  94.     /** 
  95.      * 从文件中读取数据,返回类型是字符串String类型 
  96.      *  
  97.      * @param file 
  98.      *            文件路径 
  99.      * @param charset 
  100.      *            读取文件时使用的字符集,如utf-8、GBK等 
  101.      * @return 
  102.      */  
  103.     public static String readString(String file, String charset) {  
  104.         byte[] data = readBytes(file);  
  105.         String ret = null;  
  106.   
  107.         try {  
  108.             ret = new String(data, charset);  
  109.         } catch (Exception e) {  
  110.             System.out.println(e.getMessage());  
  111.         }  
  112.         return ret;  
  113.     }  
  114.   
  115. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值