上一篇說了float類型的保存:java 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();
}
}
順便說一下追加保存
/**
* 追加文件:使用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();
}
}
}