res/raw फोल्डर से टेक्स्ट फ़ाइल पढ़ने का पूरा उदाहरण
एंड्रॉइड में res/raw/ फोल्डर में रखी टेक्स्ट फ़ाइलों को InputStream और BufferedReader के माध्यम से पढ़ा जा सकता है। यह रॉ रिसोर्सेज एपीके में अनकंप्रेस्ड रहते हैं और रनटाइम पर एक्सेसिबल होते हैं।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में प्रोजेक्ट बनाएं।
चरण 2: raw फोल्डर बनाएं और टेक्स्ट फ़ाइल जोड़ें
- प्रोजेक्ट व्यू में
resपर राइट-क्लिक → New → Android Resource Directory - Resource type: raw चुनें
res/raw/मेंsample.txtफ़ाइल बनाएं- इसमें कुछ टेक्स्ट डालें, जैसे:
यह एक नमूना टेक्स्ट फ़ाइल है। दूसरी पंक्ति। तीसरी पंक्ति जिसमें हिन्दी टेक्स्ट है। Special characters: !@#$%^&*() Numbers: 1234567890
चरण 3: लेआउट फ़ाइल
res/layout/activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:gravity="center"
android:text="Raw रिसोर्स से टेक्स्ट फ़ाइल पढ़ना"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/btnReadFile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:onClick="readTextFile"
android:text="टेक्स्ट फ़ाइल पढ़ें" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/btnReadFile"
android:layout_marginTop="16dp">
<TextView
android:id="@+id/tvFileContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
tools:text="फ़ाइल कंटेंट यहाँ दिखेगा" />
</ScrollView>
</RelativeLayout>
चरण 4: MainActivity में फ़ाइल पढ़ने का लॉजिक
package com.app.sample;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
private TextView tvFileContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvFileContent = findViewById(R.id.tvFileContent);
}
public void readTextFile(View view) {
StringBuilder stringBuilder = new StringBuilder();
try {
// R.raw.sample से InputStream प्राप्त करें (एक्सटेंशन .txt न लिखें)
InputStream inputStream = getResources().openRawResource(R.raw.sample);
// BufferedReader के साथ लाइन दर लाइन पढ़ें
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
reader.close();
inputStream.close();
// UI पर दिखाएं
tvFileContent.setText(stringBuilder.toString());
// टोस्ट में भी दिखाएं (लंबा टेक्स्ट कट सकता है)
Toast.makeText(this, "फ़ाइल पढ़ी गई: " + stringBuilder.length() + " वर्ण", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
tvFileContent.setText("त्रुटि: " + e.getMessage());
Toast.makeText(this, "फ़ाइल पढ़ने में त्रुटि: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
Kotlin में सरल संस्करण
fun readTextFile(view: View) {
try {
val inputStream = resources.openRawResource(R.raw.sample)
val content = inputStream.bufferedReader().use { it.readText() }
tvFileContent.text = content
Toast.makeText(this, "फ़ाइल पढ़ी गई", Toast.LENGTH_SHORT).show()
} catch (e: IOException) {
tvFileContent.text = "त्रुटि: ${e.message}"
}
}
चरण 5: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.app.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
महत्वपूर्ण बिंदु
- R.raw.sample - फ़ाइल नाम बिना एक्सटेंशन के (sample.txt → R.raw.sample)
- openRawResource() - InputStream लौटाता है
- InputStreamReader - एन्कोडिंग निर्दिष्ट करने के लिए (UTF-8 अनुशंसित)
- BufferedReader - कुशल लाइन-बाय-लाइन पढ़ने के लिए
- बड़ी फ़ाइलों के लिए
use(Kotlin) याtry-with-resources(Java 7+) का उपयोग करें
अन्य फ़ाइल स्थान
| स्थान | पढ़ने की विधि | उपयोग |
|---|---|---|
res/raw/ | openRawResource(R.raw.file) | संकलित, रीड-ओनली, एपीके में |
assets/ | assets.open("file.txt") | सबफोल्डर सपोर्ट, मूल फ़ाइल नाम |
| Internal Storage | openFileInput("file.txt") | ऐप-प्राइवेट, रनटाइम लिखने योग्य |
| External Storage | File(Environment.getExternalStorageDirectory(), "file.txt") | साझा स्टोरेज, अनुमति आवश्यक |