信息读取:
private String getfileMessage(int position) {
String info = null;try {
File file = new File(getExternalFilesDir(null).getAbsolutePath()
+ "/" + MyURL_Flag.survId);
File selectfile = new File(file, listfile.get(position).getName());
BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream(selectfile), "utf-8"));
while (br.ready()) {
info = br.readLine();
}
Log.e("info", info.toString()+ "---------");
br.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
return info;
}
getExternalFilesDir(null).getAbsolutePath()获取目录,listfile.get(position).getName()文件名
写人:
/**
* @author dpl
* 保存数据 ,并记录上传成功与否的状态
*
* @param c
* */
private void saveData(boolean upload_flag, JSONObject js, Context c) {
try {
StringBuffer sb = new StringBuffer();
sb.append(js.toString());
// 本地文件命名
File file = new File(c.getExternalFilesDir(null).getAbsolutePath()
+ "/" + MyURL_Flag.survId);
// 如果文件夾不存就新建一個,用surveyId命名的文件夾
if (!file.exists()) {
file.mkdirs();
}
File dateFile = new File(file, js.getString("refNo") + "_"
+ js.getString("surDate") + "_"
+ js.getString("surFromTime") + "|" + upload_flag + ".txt");
// file.getName().endsWith("|false.txt");
FileOutputStream fos = new FileOutputStream(dateFile);
OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
Log.e("dddd", sb.toString());
osw.write(sb.toString());
osw.flush();
if (!upload_flag) {
Toast.makeText(c, "數據已保存本地", 0).show();
}
// Log.e("------>", "" + len);
fos.close();
osw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.e("oooo", 123 + "");
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(c, "写入文件有误", 0).show();
Log.e("oooo", 1233 + "");
} catch (JSONException e) {
e.printStackTrace();
}
}
boolean upload_flag 上传成功与否的状态 ,JSONObject js要上传的内容