Android File Cache Tool class

Source: Internet
Author: User

/**
* 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.