안드로이드 캐시(Cache) 사용하기
2017. 1. 12. 10:02ㆍAndroid
반응형
public class Cache {
Context context;
public Cache(Context co){
context = co;
}
public File getCacheDir(Context context) {
File cacheDir = null;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
cacheDir = new File(Environment.getExternalStorageDirectory(), "cachefolder");
if(!cacheDir.isDirectory()) {
cacheDir.mkdirs();
}
}
if(!cacheDir.isDirectory()) {
cacheDir = context.getCacheDir();
}
return cacheDir;
}
public void Write(String obj) throws IOException {
File cacheDir = getCacheDir(context);
File cacheFile = new File(cacheDir, "Cache.txt");
if(!cacheFile.exists())cacheFile.createNewFile();
FileWriter fileWriter = new FileWriter(cacheFile);
fileWriter.write(obj);
fileWriter.flush();
fileWriter.close();
}
public String Read() throws IOException {
File cacheDir = getCacheDir(context);
File cacheFile = new File(cacheDir, "Cache.txt");
if(!cacheFile.exists())cacheFile.createNewFile();
FileInputStream inputStream = new FileInputStream(cacheFile);
Scanner s = new Scanner(inputStream);
String text="";
while(s.hasNext()){
text+=s.nextLine();
}
inputStream.close();
return text;
}
}
출처: http://boxfoxs.tistory.com/275 [박스여우 - BoxFox]
반응형
'Android' 카테고리의 다른 글
웹뷰(2) (0) | 2018.06.08 |
---|---|
웹뷰(1) (0) | 2018.06.08 |
XML 파서: XmlPullParser (0) | 2016.11.29 |
Android Google Chart 사용하기 (0) | 2016.11.25 |
안드로이드에서 백버튼 두 번 눌러 종료하기 (0) | 2016.10.21 |