To create an entity that can be saved in a JSON file format
Public Crime (Jsonobject json) throws jsonexception{
mId = uuid.fromstring (json.getstring (json_id));
if (Json.has (json_title))
Mtitle = json.getstring (json_title);
msolved = Json.getboolean (json_solved);
Mdate = new Date (Json.getlong (json_date));
}
/**
* Converts crime object data into Jsonobject object data that can be written to the JSON file.
* * Public
Jsonobject Tojson () throws jsonexception{
jsonobject json = new Jsonobject ();
Json.put (json_id, mid.tostring ());
Json.put (Json_title, mtitle);
Json.put (json_solved, msolved);
Json.put (Json_date, Mdate.gettime ());
return JSON;
}
Create a save and read class for a JSON file
Package com.huangfei.criminalintent;
Import Java.io.BufferedReader;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
Import Java.io.Writer;
Import java.util.ArrayList;
Import Org.json.JSONArray;
Import org.json.JSONException;
Import Org.json.JSONTokener;
Import Android.content.Context;
/** * JSON file Save and read/public class Criminalintentjsonserializer {private context mcontext;
Private String Mfilename;
Public Criminalintentjsonserializer (Context context, String filename) {super ();
Mcontext = context;
Mfilename = filename; /** * JSON file read * @throws jsonexception/public arraylist<crime> loadcrimes () throws IO
Exception, jsonexception{arraylist<crime> crimes = new arraylist<crime> ();
BufferedReader reader = null;
try { FileInputStream in = Mcontext.openfileinput (Mfilename);
reader = new BufferedReader (new InputStreamReader (in));
StringBuilder jsonstring = new StringBuilder ();
String line = null;
while (line = Reader.readline ())!= null) {jsonstring.append (line);
} jsonarray array = (Jsonarray) New Jsontokener (Jsonstring.tostring ()). NextValue ();
for (int i = 0; i < array.length (); i++) {Crimes.add (New Crime (Array.getjsonobject (i)));
} catch (FileNotFoundException e) {e.printstacktrace ();
finally {if (reader!= null) reader.close ();
return crimes; /** * JSON File Save */public void Savecrimes (arraylist<crime> crimes) throws Jsonexception, Ioexce
ption {Jsonarray array = new Jsonarray (); for (Crime crime:crimes) {array.put (Crime.Tojson ());
} Writer Writer = null; try {/** * context.openfileoutput (...) method receives a file name (cannot be a directory that cannot be included/) and a file operation mode parameter, automatically attaches the incoming file name to the application sandbox file * (each applied sandbox file directory is a subdirectory of the device/data/data/directory, and the default is named by the application package).
such as:/data/data/com.huangfei.criminalintent. * Only if the device is root, other applications and users can get the files in the sandbox at random.
) * After the directory path, and then creates and opens the file under the new path, waiting for the data to be written.
* * FileOutputStream out = Mcontext.openfileoutput (Mfilename, context.mode_private);
writer = new OutputStreamWriter (out);
Writer.write (Array.tostring ());
} finally{if (writer!= null) writer.close ();
}
}
}
the reading and saving of JSON files
Mserializer = new Criminalintentjsonserializer (Mappcontext, FILENAME);
try {
mcrimes = Mserializer.loadcrimes ();
} catch (Exception e) {
mcrimes = new arraylist<crime> ();
e.printstacktrace ();
}
public Boolean savecrimes () {
try {
mserializer.savecrimes (mcrimes);
LOG.D (TAG, "crimes saved to File");
return true;
} catch (Exception e) {
log.e (TAG, "Error Saving Crimes:", e);
return false;
}
}
the basic file or directory processing method provided by the context class
Method |
Purpose of Use |
File Getfiledir () |
Get/data/data/packagename/files Directory |
FileInputStream openfileinput (String name) |
Open an existing file for reading |
FileOutputStream openfileoutput (String name, int mode) |
Open a file for writing and create it if it does not exist |
File Getdir (String name, int mode) |
Gets the subdirectory of the/data/data/packagename/directory (creates it if it does not exist) |
String[] FileList () |
Get a list of files in the/data/data/packagename/files directory |
File Getcachedir () |
Get/data/data/packagename/cache Directory |
Code address