上一篇说了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();
}
}
}