21-Amaliy ish
Download 365.94 Kb.
|
21-compressed
android.content.Context abstrakt sinfi fayllar bilan ishlashning bir qator metodlarini aniqlaydi:
deleteFile(String name): faylni o'chiradi. fileList(): ilova katalogidagi /files katalogidagi barcha fayllarni oladi. getCacheDir(): ilova katalogidagi cache katalogini oladi. getDir(String dirName, int mode): ilova katalogidagi ma'lum bir katalogni oladi, agar bunday katalog bo'lmasa, u yaratiladi. getExternalCacheDir(): qurilmaning tashqi fayl tizimidagi /cache papkasini oladi. getExternalFilesDir(): qurilmaning tashqi fayl tizimining /files katalogini oladi. getFileStreamPath(String filename): Fayl tizimidagi faylga absolyut yo'lni qaytaradi. openFileInput(String filename): o'qish uchun faylni ochadi. openFileOutput(String name, int mode): yozish uchun faylni ochadi. Ilovada yaratilgan va tahrir qilingan barcha fayllar odatda ilova katalogidagi /files katalogida saqlanadi. java.io paketidan standart Java sinflari fayllarni o'qish va yozish uchun ishlatiladi. Shunday qilib, ilovada fayllarni o'qish-yozish funksiyasidan foydalanaylik. Android Studio-da yangi ilova yarating va "Fayllar" deb nomlang. activity_main.xml faylini quyidagicha o'zgartiring: android:layout_height="match_parent" android:orientation="vertical"> android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="4" android:gravity="start" /> android:layout_width="wrap_content" android:layout_height="0dp" android:layout_gravity="center" android:layout_margin="16dp" android:layout_weight="1" android:onClick="saveText" android:text="Saqlash" /> android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="80dp" android:layout_weight="4" android:gravity="start" /> android:layout_width="wrap_content" android:layout_height="0dp" android:layout_gravity="center" android:layout_margin="16dp" android:layout_weight="1" android:onClick="openText" android:text="Ochish" /> EditText maydoni matn kiritish uchun, TextView esa avval saqlangan matnni namoyish qilish uchun. Matnni saqlash va ko'rsatish uchun ikkita tugma qo'shilgan. Endi Activity kodida tugmachalarni bosganda faylni saqlash va o'qish uchun quyidagi kodni yozing: package com.example.android.fayllar; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class MainActivity extends AppCompatActivity { private final static String FILE_NAME = "content.txt"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } // faylni saqlash public void saveText(View view) { FileOutputStream fos = null; try { EditText textBox = findViewById(R.id.save_text); String text = textBox.getText().toString(); fos = openFileOutput(FILE_NAME, MODE_PRIVATE); fos.write(text.getBytes()); Toast.makeText(this, "Fayl saqlandi", Toast.LENGTH_SHORT).show(); } catch (IOException ex) { Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show(); } finally { try { if (fos != null) fos.close(); } catch (IOException ex) { Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show(); } } } // faylni ochish public void openText(View view) { FileInputStream fin = null; TextView textView = findViewById(R.id.open_text); try { fin = openFileInput(FILE_NAME); byte[] bytes = new byte[fin.available()]; fin.read(bytes); String text = new String(bytes); textView.setText(text); } catch (IOException ex) { Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show(); } finally { try { if (fin != null) fin.close(); } catch (IOException ex) { Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show(); } } } } Saqlash tugmachasini bosish FileOutputStream fos = openFileOutput(FILE_NAME, MODE_PRIVATE) chiqish oqimini hosil qiladi. Bizni kodda, kiritilgan matn "content.txt" faylida saqlanadi. Bunda MODE_PRIVATE rejimidan foydalanilgan. Tizim ikki xil rejimdagi fayllarni yaratishga imkon beradi: Download 365.94 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2025
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling