Bu dersimizde internal storage’daki dosyaların içeriğini nasıl göreceğimizden ve bu dosyaları nasıl sileceğimizden bahsediyoruz. Ayrıca internal storageda oluşturduğumuz dosyaların klasör yolunu ve bu klasördeki tüm dosyaları nasıl listeleyebileceğimizi de öğrenmiş olacağız.
Örneğimizde dosya içeriğini görmek için farklı bir activity kullandık. Bu arayüzde adını verdiğimiz dosyanın içeriğini butona tıklanıldığında ekranda gösteriyoruz. Peki bunu nasıl yapıyoruz ?
Bir dosya içeriğini okumak için openFileInput(String dosyaadi) methodunu kullanıyoruz. Bu method geriye bir FileInputStream nesnesi döndürür ve biz bu nesne sayesinde açtığımız dosyanın içeriğini okur ve de bu bağlantıyı kapatabiliriz.
Dosyadan verileri okurken fis.read() methodundan faydalanırız. Bu method byte olarak okuduğu verileri int değerlere çevirip geriye döndürür. Bu yüzden while döngüsü kullanarak -1 değerini yani dosya sonu belirtecini görene kadar çalışmasını sağlarız. Aldığımız değerleri bir stringbuffer nesnesine append methodunu kullanarak sırasıyla ekleriz. fis.read() methodu int değerler döndürdüğü için bunları buffera eklemeden önce char veri tipine dönüştürmemiz gerekiyor. Döngü tamamlandığında dosyamızın tüm içeriği buffer değişkeninde saklanmış olur.
1 2 3 |
while ((read = fis.read()) != -1) { buffer.append((char) read); } |
Yine işlemler bittiğinde FileInputStream’i close() methoduyla kapatmayı unutmayalım.
getFilesDir() : Bu method internal storage’a kaydettiğimiz dosyaların tam yolunu bize verir.
fileList() : Bu method internal storage’daki tüm dosya isimlerini bir string dizisi şeklinde geriye döndürür. Örneğimizde aldığımız tüm dosya isimlerini StringBuilder kullanarak aralarında virgül olacak şekilde tek bir string nesnesine ekledik.
for( String dosya : dosyalarListesi ){
stringBuilder.append(dosya).append(” ,”);
}
deleteFile(String dosya_adi) : Bu method parametre olarak dosya adını alır ve bu dosyayı siler. Dosyayı bulup başarılı bir şekilde sildiyse true, dosyayı bulamaz veya silme işlemi sırasında bir sorun çıkarsa false değer döndürür.
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 |
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void openInternalStorageDemoActivity(View view) { Intent intent=new Intent(this, InternalStorageDemo.class); startActivity(intent); } } |
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 |
public class InternalStorageDemo extends AppCompatActivity { private EditText etFileName, etMessage, etFileToDelete; private TextView txvInternalStoragePath, txvFilesList; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.internal_storage); etFileName = (EditText) findViewById(R.id.etFileName); etMessage = (EditText) findViewById(R.id.etMessage); etFileToDelete = (EditText) findViewById(R.id.etFileToDelete); txvInternalStoragePath = (TextView) findViewById(R.id.txvInternalStoragePath); txvFilesList = (TextView) findViewById(R.id.txvFilesList); } public void saveToInternalStorage(View view) { String dosyaAdi=etFileName.getText().toString(); String data=etMessage.getText().toString(); FileOutputStream fos=null; try { fos=openFileOutput(dosyaAdi, MODE_PRIVATE); fos.write(data.getBytes()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(fos!=null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void showInternalStoragePath(View view) { String dosyaYolu= ""+ getFilesDir(); txvInternalStoragePath.setText(dosyaYolu); } public void showFilesList(View view) { String[] dosyalarListesi=fileList(); StringBuilder stringBuilder=new StringBuilder(); for(String dosya : dosyalarListesi){ stringBuilder.append(dosya).append(" ,"); } txvFilesList.setText(stringBuilder); } public void deleteFile(View view) { String silinecekDosya=etFileToDelete.getText().toString(); boolean silindi=deleteFile(silinecekDosya); if(silindi==true){ Toast.makeText(this, "Dosya Silindi", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this, "Dosya Silinemedi", Toast.LENGTH_SHORT).show(); } } public void moveToDisplayScreen(View view) { Intent intent=new Intent(this, InternalStorageGoruntule.class); startActivity(intent); } } |
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 99 100 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp" tools:context="com.example.emrealtunbilek.androidfilesystemornek.InternalStorageDemo"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/etFileName" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="dosyaAdı" /> <EditText android:id="@+id/etMessage" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Mesaj" /> </LinearLayout> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="saveToInternalStorage" android:text="Veriyi Dosyaya Kaydet" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="moveToDisplayScreen" android:text="Dosyaları Görüntüle" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="70dp" android:onClick="showInternalStoragePath" android:text="Internal Storage Dosya Yolu" /> <TextView android:id="@+id/txvInternalStoragePath" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="Internal Storage Yolu" android:textColor="@android:color/black" android:textSize="15sp" android:textStyle="italic" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:onClick="showFilesList" android:text="Internal Storagedaki Dosya Listesi" /> <TextView android:id="@+id/txvFilesList" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="Dosyaların Adları" android:textColor="@android:color/black" android:textSize="15sp" android:textStyle="italic" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="deleteFile" android:text="Dosyayı Sil" /> <EditText android:id="@+id/etFileToDelete" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Silinecek Dosya Adı" /> </LinearLayout> </LinearLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp" tools:context="com.example.emrealtunbilek.androidfilesystemornek.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Internal Storage Demo" android:onClick="openInternalStorageDemoActivity"/> </LinearLayout> |
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 |
public class InternalStorageGoruntule extends AppCompatActivity { private EditText etFileName; private TextView txvFileContent; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.internal_storage_goruntule); etFileName = (EditText) findViewById(R.id.etFileName); txvFileContent = (TextView) findViewById(R.id.txvFileContent); } public void readAndDisplayFromFile(View view) { String dosyaAdi = etFileName.getText().toString(); StringBuffer buffer = new StringBuffer(); FileInputStream fis = null; try { fis = openFileInput(dosyaAdi); int read; while ((read = fis.read()) != -1) { /* Log.i("emre", ""+read); Log.i("emre", ""+(char)read); */ 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(); } } } txvFileContent.setText(buffer); } } |
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 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/etFileName" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="dosya_adi"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Dosyayı Göster" android:onClick="readAndDisplayFromFile"/> </LinearLayout> <TextView android:id="@+id/txvFileContent" android:layout_width="match_parent" android:layout_height="match_parent" android:textSize="20sp" android:text="Dosya İçeriği" android:layout_margin="10dp"/> </LinearLayout> |