public class Uit {
Context s;
private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm");
public Uit(Context s){
this.s=s;
}
public String saveCrashInfo2File(String json) {
StringBuffer sb = new StringBuffer();
Writer writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
printWriter.close();
String result = writer.toString();
sb.append(result);
try {
String time = formatter.format(new Date());
String fileName = "demo-" + time + ".txt";
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String path = Environment.getExternalStorageDirectory()+"/msm/path/";
File dir = new File(path);
if(!dir.exists()){
dir.mkdirs();
}
FileOutputStream fos = new FileOutputStream(path + fileName);
fos.write(json.getBytes());
fos.close();
}
return fileName;
} catch (Exception e) {
Toast.makeText(s,"失败",Toast.LENGTH_LONG).show();
}
return null;
}
}
在调用后会在手机根目录下的msm/path/生成以时间为名称的txt文件,可以用来记录json数据,