To read a .txt file from the drawable folder or any other folder in an Android app using FileInputStream
, you need to first copy the file to the internal storage of the app and then read it. Here’s how you can do it:
-
Copy the File to Internal Storage:
مقالات ذات صلة-
كيفية استخراج ملف .tar.gz10/03/2024
- First, create a method to copy the file from the drawable folder to the internal storage of your app:
javaprivate void copyFileToInternalStorage() { try { InputStream inputStream = getResources().openRawResource(R.drawable.your_txt_file); FileOutputStream fileOutputStream = openFileOutput("your_txt_file.txt", Context.MODE_PRIVATE); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) > 0) { fileOutputStream.write(buffer, 0, length); } inputStream.close(); fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } }
- Replace
R.drawable.your_txt_file
with the resource ID of your .txt file in thedrawable
folder.
-
Read the File and Set it in TextView:
- Next, read the file from the internal storage and set it in a TextView:
javaprivate void readFileAndSetInTextView() { try { FileInputStream fileInputStream = openFileInput("your_txt_file.txt"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream)); StringBuilder stringBuilder = new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) { stringBuilder.append(line).append("\n"); } TextView textView = findViewById(R.id.textView); textView.setText(stringBuilder.toString()); bufferedReader.close(); fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } }
- Make sure to call
copyFileToInternalStorage()
before callingreadFileAndSetInTextView()
to ensure that the file is copied to the internal storage.
-
Permissions:
- Don’t forget to add the necessary permissions in your
AndroidManifest.xml
file:
xml<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- Don’t forget to add the necessary permissions in your
-
Usage:
- Call
readFileAndSetInTextView()
in your activity’sonCreate()
method to read the file and set it in the TextView.
- Call
This approach copies the file from the drawable folder to the internal storage for reading, as direct access to files in the drawable
folder is not possible.
المزيد من المعلومات
قراءة ملف نصي (.txt) من مجلد drawable أو أي مجلد آخر في تطبيق Android يتطلب القليل من الخطوات الإضافية بسبب الفروق في طرق الوصول إلى الملفات في المجلدات المختلفة.
-
تحميل الملف إلى الذاكرة الداخلية:
- يمكنك استخدام الرمز التالي لنسخ الملف من مجلد drawable إلى الذاكرة الداخلية للتطبيق:
javaprivate void copyFileToInternalStorage() { try { InputStream inputStream = getResources().openRawResource(R.drawable.your_txt_file); FileOutputStream fileOutputStream = openFileOutput("your_txt_file.txt", Context.MODE_PRIVATE); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) > 0) { fileOutputStream.write(buffer, 0, length); } inputStream.close(); fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } }
- قم بتعويض
R.drawable.your_txt_file
بمعرف المورد الخاص بملف .txt في مجلدdrawable
.
-
قراءة الملف وتعيينه في TextView:
- بعد ذلك، قم بقراءة الملف من الذاكرة الداخلية وتعيينه في TextView:
javaprivate void readFileAndSetInTextView() { try { FileInputStream fileInputStream = openFileInput("your_txt_file.txt"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream)); StringBuilder stringBuilder = new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) { stringBuilder.append(line).append("\n"); } TextView textView = findViewById(R.id.textView); textView.setText(stringBuilder.toString()); bufferedReader.close(); fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } }
- تأكد من استدعاء
copyFileToInternalStorage()
قبل استدعاءreadFileAndSetInTextView()
لضمان نسخ الملف إلى الذاكرة الداخلية.
-
الصلاحيات:
- لا تنسَ إضافة الصلاحيات اللازمة في ملف
AndroidManifest.xml
:
xml<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- لا تنسَ إضافة الصلاحيات اللازمة في ملف
-
استخدام:
- قم بإستدعاء
readFileAndSetInTextView()
في طريقةonCreate()
في النشاط الخاص بك لقراءة الملف وتعيينه في TextView.
- قم بإستدعاء
هذه الطريقة تنسخ الملف من مجلد drawable إلى الذاكرة الداخلية للقراءة، لأن الوصول المباشر إلى الملفات في مجلد drawable
غير ممكن.