567
社区成员




package com.joboevan.push.tool;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import android.content.Context;
/**
* 书写一个写入内部文件的类
*
* @author lw
*
*/
public class MyWriter {
private BufferedWriter writer;
public MyWriter(Context activity, String fileName, boolean isApp) {
try {
FileOutputStream os = null;
if (isApp) {
os = activity.openFileOutput(fileName,
Context.MODE_WORLD_READABLE
+ Context.MODE_WORLD_WRITEABLE
+ Context.MODE_APPEND);
} else {
os = activity.openFileOutput(fileName,
Context.MODE_WORLD_READABLE
+ Context.MODE_WORLD_WRITEABLE);
}
writer = new BufferedWriter(new OutputStreamWriter(os));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
// 书写信息
public void writerMsg(String msg) {
if (writer == null) {
return;
}
try {
writer.write(msg);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
JSON存储,调用方式
public static void setJSONValue(Context context, String key, String value) {
String strValue = new MyReader(context, "refresh.temp").readerMsg();
// 构建JSON
JSONObject jsonObject = null;
if (strValue == null) {
jsonObject = new JSONObject();
} else {
try {
jsonObject = new JSONObject(strValue);
} catch (JSONException e) {
jsonObject = new JSONObject();
}
}
// 储存当前的值
try {
if (value == null && key == null) {
new MyWriter(context, "refresh.temp", false).writerMsg("");
} else {
if (value == null)
jsonObject.remove(key);
else
jsonObject.put(key, value);
String jsoStr = jsonObject.toString();
new MyWriter(context, "refresh.temp", false).writerMsg(jsoStr);
}
} catch (JSONException e) {
e.printStackTrace();
}
}