开发过程中文件的读写

信息读取:

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要上传的内容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值