Internal va External Storage�Fayl yaratish, o‘qish/yozish, permissionlar bilan ishlash�
Android’da fayllar bilan ishlash:�
1. Internal Storage (Ichki xotira)�
/data/data/<app-package>/files/
Foydalanish:�
FileOutputStream fos = openFileOutput("data.txt", MODE_PRIVATE); fos.write("Hello Internal Storage".getBytes());
fos.close();
O‘qish:
FileInputStream fis = openFileInput("data.txt");
External Storage (Tashqi xotira)�
Asosiy xususiyatlar:
External Storage yo‘llari:
Android 10+ (API 29) dan boshlab: Scoped Storage
O‘zgarishlar:
Scoped Storage sababli eski WRITE_EXTERNAL_STORAGE ruxsati ko‘p holatda ishlatilmaydi.
4. Runtime Permissions (Android 6+)�Foydalanuvchi permissionni run-time da tasdiqlashi kerak.�Permission so‘rash (modern API):
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
100
);
}
Amaliy misollar:
public void writeInternal(String text) {
try {
FileOutputStream fos = openFileOutput("mytext.txt", MODE_PRIVATE);
fos.write(text.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
2. O‘qish:
public String readInternal() {
StringBuilder sb = new StringBuilder();
try {
FileInputStream fis = openFileInput("mytext.txt");
int ch;
while ((ch = fis.read()) != -1) {
sb.append((char) ch);
}
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
B) EXTERNAL STORAGE: Fayl yaratish, yozish, o‘qish�1. Permission qo‘shish (AndroidManifest.xml)�
2. Yozish (App-specific directory)
public void writeExternal(String text) {
File path = getExternalFilesDir(null); // app folder
File file = new File(path, "notes.txt");
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(text.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}