android byte格式文件保存,追加保存

上一篇说了float类型的保存:Android 保存float数组 大端转小端
里面说到了大端小端的问题,如果用byte格式保存就可以完美避免这个问题。

public static void saveByte(byte[] bytes, String str)
{
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(str);
        fos.write(bytes, 0, bytes.length);
        fos.flush();
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
// 也可以这么写
public static void saveByteToBin(byte[] bytes, String vPath) {
    FileOutputStream fos;
    DataOutputStream dos;
    try {
        fos = new FileOutputStream(vPath);
        dos = new DataOutputStream(fos);
        dos.write(bytes);
        dos.flush();
        fos.flush();
        dos.close();
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

顺便说一下追加保存

/**
	 * 追加文件:使用FileWriter
	 *
	 * @param fileName
	 * @param content
	 */
public static void writeFileAdd(String fileName, String content) {
    FileWriter writer = null;
    try {
        // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
        writer = new FileWriter(fileName, true);
        writer.write(content);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if(writer != null){
                writer.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值