android cache 사용하기
android cache 사용하기
---------------------------------------------------
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import android.content.Context;
public class CacheStorage {
private static String _cacheFileName = "cache";
private static Context Ctx;
public CacheStorage(Context ctx, String cacheFileName){
if(cacheFileName != null && cacheFileName.length() > 0)
_cacheFileName = cacheFileName;
}
public void setCacheFile(String data) throws IOException{
setCacheFile( _cacheFileName, data);
}
public void setCacheFile(String cacheFileName, String data) throws IOException{
if(cacheFileName == null || cacheFileName.length() < 1) cacheFileName = _cacheFileName;
try {
File cacheDir = Ctx.getCacheDir();
File cacheFile = new File(cacheDir.getAbsolutePath(), cacheFileName);
FileOutputStream fos;
fos = new FileOutputStream(cacheFile.getAbsolutePath());
fos.write(data.getBytes());
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public String getCacheFile(){
return getCacheFile(_cacheFileName);
}
public String getCacheFile(String cacheFileName){
File cacheDir = Ctx.getCacheDir();
File cacheFile = new File(cacheDir.getAbsolutePath(), cacheFileName);
if(!cacheFile.exists()) return "";
BufferedReader br;
StringBuilder sb = new StringBuilder(128);
try {
FileInputStream fis = new FileInputStream(cacheFile);
br = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
public boolean exists(){
File cacheDir = Ctx.getCacheDir();
File cacheFile = new File(cacheDir.getAbsolutePath(), _cacheFileName);
return cacheFile.exists();
}
public boolean deleteCacheFile(String cacheFileName){
File cacheDir = Ctx.getCacheDir();
File cacheFile = new File(cacheDir.getAbsolutePath(), cacheFileName);
if(!cacheFile.exists()) return false;
return cacheFile.delete();
}
}
---------------------------------------------------
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import android.content.Context;
public class CacheStorage {
private static String _cacheFileName = "cache";
private static Context Ctx;
public CacheStorage(Context ctx, String cacheFileName){
if(cacheFileName != null && cacheFileName.length() > 0)
_cacheFileName = cacheFileName;
}
public void setCacheFile(String data) throws IOException{
setCacheFile( _cacheFileName, data);
}
public void setCacheFile(String cacheFileName, String data) throws IOException{
if(cacheFileName == null || cacheFileName.length() < 1) cacheFileName = _cacheFileName;
try {
File cacheDir = Ctx.getCacheDir();
File cacheFile = new File(cacheDir.getAbsolutePath(), cacheFileName);
FileOutputStream fos;
fos = new FileOutputStream(cacheFile.getAbsolutePath());
fos.write(data.getBytes());
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public String getCacheFile(){
return getCacheFile(_cacheFileName);
}
public String getCacheFile(String cacheFileName){
File cacheDir = Ctx.getCacheDir();
File cacheFile = new File(cacheDir.getAbsolutePath(), cacheFileName);
if(!cacheFile.exists()) return "";
BufferedReader br;
StringBuilder sb = new StringBuilder(128);
try {
FileInputStream fis = new FileInputStream(cacheFile);
br = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
public boolean exists(){
File cacheDir = Ctx.getCacheDir();
File cacheFile = new File(cacheDir.getAbsolutePath(), _cacheFileName);
return cacheFile.exists();
}
public boolean deleteCacheFile(String cacheFileName){
File cacheDir = Ctx.getCacheDir();
File cacheFile = new File(cacheDir.getAbsolutePath(), cacheFileName);
if(!cacheFile.exists()) return false;
return cacheFile.delete();
}
}