/**
* Tool classes for JSON data caching
*
*/
public class CACHEDATASD {
/**
*
* @param Context Current Object
* @param files created by dir
* @param requesturl Flag field
* @param jsondata JSON data
*/
public static void Savesdbytearray (context context, String dir,
String Requesturl, String jsondata) {
String Directory=context.getexternalcachedir () +file.separator +dir+file.separator;
First determine whether the directory exists, and then determine whether the file exists
Load Complete for storage
File Dirs=new file (Directory);
File Cac=new file (Dirs,requesturl);
if (dirs.exists () && dirs.length () >0) {
if (cac.exists () && cac.length () >0) {
Do not do any action
}else{
try {
byte[] data = jsondata.getbytes ("Utf-8");
FileOutputStream Outf;
Outf=new FileOutputStream (Directory+requesturl);
Bufferedoutputstream bufferout = new Bufferedoutputstream (OUTF);
Bufferout.write (data);
Bufferout.flush ();
Bufferout.close ();
} catch (Exception e) {
E.printstacktrace ();
}
}
}else{
Dirs.mkdirs ();
try {
byte[] data = jsondata.getbytes ("Utf-8");
FileOutputStream Outf;
Outf=new FileOutputStream (Directory+requesturl);
Bufferedoutputstream bufferout = new Bufferedoutputstream (OUTF);
Bufferout.write (data);
Bufferout.flush ();
Bufferout.close ();
} catch (Exception e) {
E.printstacktrace ();
}
}
}
/**
*
* @param Context Current Object
* @param files created by dir
* @param requesturl Flag field
* @param jsondata JSON data
*/
public static byte[] Readsdbytearray (context context, String dir,
String Requesturl) {
Bytearrayoutputstream out;
try {
Bufferedinputstream in = new Bufferedinputstream (
New FileInputStream (Context.getexternalcachedir () +file.separator + dir + file.separator +requesturl));
out = new Bytearrayoutputstream (1024);
byte[] temp = new byte[1024];
int size = 0;
while (size = In.read (temp))! =-1) {
Out.write (temp, 0, size);
}
In.close ();
byte[] content = Out.tobytearray ();
return content;
} catch (Exception e) {
E.printstacktrace ();
return null;
}
}
}
Android File Cache Tool class