Android 保存float数组 大端转小端

1、用二进制保存到文件

public void saveFloatArray2Bin(float[] vArr, String vPath) {
    FileOutputStream fos = null;
    DataOutputStream dos = null;
    try {
        // 新建文件流
        fos = new FileOutputStream(vPath);
        // 新建数据流关联文件流
        dos = new DataOutputStream(fos);

        // 读取每个float到流里
        for (float f:vArr) {
            // 写入到文件流里
            dos.writeFloat(f);
        }

        // force bytes to the underlying stream
        dos.flush();
        fos.flush();

        dos.close();
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

以上方式有个致命缺点,那就是存的慢!为什么呢?因为在重复的开流存储,这是耗时操作。由于Android写float数据只提供writeFloat()这个接口,想要把速度提上去就要先把float数据转成byte数组,全部转完以后再用存byte数组的接口进行写操作。
以下是先把short转出byte数组再保存,其中要留意大小端转换。

public static void saveFloatArrayToBin(float[] vArr, String vPath) {
    FileOutputStream fos;
    DataOutputStream dos;
    try {
        fos = new FileOutputStream(vPath);
        dos = new DataOutputStream(fos);
        byte[] bytes = new byte[vArr.length*4];
        for (int i = 0;i<vArr.length;i++){
            int accum = Float.floatToRawIntBits(vArr[i]);
            bytes[i*4] = (byte)(accum & 0xFF);
            bytes[i*4+1] = (byte)((accum>>8) & 0xFF);
            bytes[i*4+2] = (byte)((accum>>16) & 0xFF);
            bytes[i*4+3] = (byte)((accum>>24) & 0xFF);
        }
        dos.write(bytes);
        dos.flush();
        fos.flush();
        dos.close();
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

2、读取float类型的二进制文件

public void readFloatFile2Float(float[] vArr, String vPath) {
    InputStream is = null;
    DataInputStream dis = null;
    try {
        is = new FileInputStream(vPath);
        dis = new DataInputStream(is);

        while(dis.available()>0)
        {
            float c = dis.readFloat();
            Log.d("float打印", "readFloatFile2Float: "+c);
        }

        is.close();
        dis.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

3、float 保存成txt文件

这个方法需要先将float转成string再保存

public void saveFloatArray2File(float[] vArr, String vPath) {
    if (null == vArr) {
        return;
    }
    if (null == vPath || vPath.equals("")) {
        return;
    }
    File file = new File(vPath);  //存放数组数据的文件
    StringBuffer tBuffer = new StringBuffer();
    for (float val : vArr) {
        //保留6位小数,这里可以改为其他值
        tBuffer.append(String.format("%.6f", val));
        tBuffer.append(",");//以逗号分隔
    }
    try {
        FileWriter out = new FileWriter(file);  //文件写入流
        out.write(tBuffer.toString());//写文件
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

4、flaot数据大端转小端

float数组保存成二进制文件的时候后需要注意区分大小端模式,大端模式下保持的文件拿到小端模式的环境里读取是会出错的。

大端模式,是指数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地址中,这样的存储模式有点儿类似于把数据当作字符串顺序处理:地址由小向大增加,而数据从高位往低位放;这和我们的阅读习惯一致。

小端模式,是指数据的高字节保存在内存的高地址中,而数据的低字节保存在内存的低地址中,这种存储模式将地址的高低和数据位权有效地结合起来,高地址部分权值高,低地址部分权值低。

//大端转小端
private float big2Little(float big){
    // 把float转换为byte[]
    int fbit = Float.floatToIntBits(big);

    byte[] b = new byte[4];
    b[0] = (byte) (fbit >> 24);
    b[1] = (byte) (fbit >> 16);
    b[2] = (byte) (fbit >> 8);
    b[3] = (byte) (fbit);
    
    int l;
    l = b[0];
    l &= 0xff;
    l |= ((long) b[1] << 8);
    l &= 0xffff;
    l |= ((long) b[2] << 16);
    l &= 0xffffff;
    l |= ((long) b[3] << 24);
    float little = Float.intBitsToFloat(l);
    return little;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值