Bu dersimizde android cache kavramından ve internal cache storage’da dosya oluşturma ve okuma işlemlerinden bahsedeceğiz.
Cache Storage : Geçici dosyaların tutulduğu alandır. Uygulama kaldırıldığında bu kısımda bulunan dosyalar da silinir. Kısıtlı bir alan olduğundan buradaki dosyaları işimiz bitince silmeliyiz. Internal Storage’da yeterli alan kalmadığında işletim sistemi yer açmak için burada bulunan dosyaları silebilir. Burdaki dosyaların büyüklüğü 1 MB ‘ı aşmamalıdır. Uygulamanın etkili bir şekilde çalışması için bu alan kullanılır.
Cache storage internal ve external olarak ikiye ayrılır. Bu derste sadece internal cache storage üzerinde işlemler yapacağız. Yapacağımız işlemlerde dosya oluşturup içeriğini okuyacağız ve bu işlemler daha önce anlatılan dosya yaratma ve okuma işlemlerinden çok da farklı değil. Bu işlemleri bilmeyen arkadaşlar önceki yazılara göz atabilir. Peki internal storage’dan ne gibi farklılıkları var?
Sadece dosyayı belirtirken farklılık vardır. Dosyaya yazma ve okuma işlemleri aynıdır.
getCacheDir() : Bu method internal cache storage’ın yolunu File tipinde geriye gönderir.
1 2 |
File cacheDizini = getCacheDir(); File cacheDosyam = new File(cacheDizini, "cache_dosyasi.txt"); |
Yukarıdaki kod sayesinde cache dizininde cache_dosyasi.txt dosyasını oluşturup, FileOutputStream ile dosyaya veri yazabiliriz.
Dosyadan okurken de aynı yol izlenir. Yani tek farklılık ilgili dizine gitmek ve dosyayı orda oluşturmakta.
Videoda bahsetmeyi unuttuğum bir konu var. Bu klasörü geçici olarak kullandıktan sonra silmemiz gerekli olabilir. Mesela uygulamadan çıkılırken ve ya activity destroy edilirken onDestroy methodu içine aşağıdaki kodlar yazılarak içindeki tüm klasör ve dosyalar silinebilir.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public static void deleteCache(Context context) { try { File dir = context.getCacheDir(); if (dir != null && dir.isDirectory()) { deleteDir(dir); } } catch (Exception e) {} } public static boolean deleteDir(File dir) { if (dir != null && dir.isDirectory()) { String[] children = dir.list(); for (int i = 0; i < children.length; i++) { boolean success = deleteDir(new File(dir, children[i])); if (!success) { return false; } } } return dir.delete(); } |
Bu derste yazılan kodlar aşağıdaki gibidir. AndroidFileSystemOrnek projesinin bitmiş son halini buradaki linkten indirebilirsiniz.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
public class CacheStorageDemo extends AppCompatActivity { private EditText etInternalCacheData, etExternalCacheData; private TextView txvInternalCacheData, txvExternalCacheData; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.cache_storage); etInternalCacheData = (EditText) findViewById(R.id.etInternalCacheData); etExternalCacheData = (EditText) findViewById(R.id.etExternalCacheData); txvInternalCacheData = (TextView) findViewById(R.id.txvInternalCacheContent); txvExternalCacheData = (TextView) findViewById(R.id.txvExternalCacheContent); } public void saveToInternalCache(View view) { String cacheData = etInternalCacheData.getText().toString(); File cacheDizini = getCacheDir(); File cacheDosyam = new File(cacheDizini, "cache_dosyasi.txt"); FileOutputStream fos = null; try { fos = new FileOutputStream(cacheDosyam); fos.write(cacheData.getBytes()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); Toast.makeText(this, "Dosyaya Yazıldı", Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); } } } } public void loadFromInternalCache(View view) { StringBuffer buffer=new StringBuffer(); File cacheDizini = getCacheDir(); File cacheDosyam = new File(cacheDizini, "cache_dosyasi.txt"); FileInputStream fis=null; try { fis=new FileInputStream(cacheDosyam); int read; while((read = fis.read()) != -1){ buffer.append((char) read); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(fis != null){ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } txvInternalCacheData.setText(buffer); } public void saveToExternalCache(View view) { } public void loadFromExternalCache(View view) { } } |