java 寫二進制文件

private void WritePointToFile(MyPoint p) {
String str = String.valueOf(p.x) + "|" + String.valueOf(p.y) + "|"
+ p.getCurrenttime() + "|" + String.valueOf(p.getRSSI()) + "|"
+ p.getAPName();
String sdPath = Environment.getExternalStorageDirectory().getPath();
File f = new File(sdPath + "/***_wifi/rssipoint/");
if (!f.exists()) {
f.mkdirs();
}
File file = new File(f.getPath(), p.getAPName()+".dat");
byte[] bt = new byte[64];
byte[] temp = str.getBytes();
for (int i = 0; i < temp.length; i++) {
bt[i] = temp[i];
}
FileOutputStream fis;
try {
if (file.exists()) {
// 如果存在相同的文件就在裏面append
fis = new FileOutputStream(f.getPath() + "/" + p.getAPName()+".dat", true);
BufferedOutputStream bos1 = new BufferedOutputStream(fis);
bos1.write(bt);
bos1.close();
fis.close();
} else {
fis = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fis);
bos.write(bt);
bos.close();
fis.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

MyPoint 爲自定義類型


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章