यह उदाहरण दर्शाता है कि Kotlin में आंतरिक स्टोरेज का उपयोग करके टेक्स्ट फ़ाइल कैसे बनाएं और उसमें डेटा लिखें।
चरण 1 - नया प्रोजेक्ट बनाएं
Android Studio: File → New Project।
चरण 2 - 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"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:background="#008080"
android:padding="5dp"
android:text="TutorialsPoint"
android:textColor="#fff"
android:textStyle="bold" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_marginTop="40dp"
android:ems="10"
android:hint="टेक्स्ट दर्ज करें">
<requestFocus />
</EditText>
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText"
android:layout_centerInParent="true"
android:layout_margin="5dp"
android:background="@android:color/holo_orange_light"
android:onClick="saveTextFile"
android:text="फ़ाइल सहेजें"
android:textColor="@android:color/background_dark"
android:textStyle="bold" />
</RelativeLayout>
चरण 3 - MainActivity.kt में फ़ाइल लेखन लॉजिक
import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import java.io.FileOutputStream
import java.io.OutputStreamWriter
class MainActivity : AppCompatActivity() {
private lateinit var editText: EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
editText = findViewById(R.id.editText)
}
fun saveTextFile(view: View) {
try {
val fileOutputStream: FileOutputStream = openFileOutput("mytextfile.txt", Context.MODE_PRIVATE)
val outputWriter = OutputStreamWriter(fileOutputStream)
outputWriter.write(editText.text.toString())
outputWriter.close()
Toast.makeText(baseContext, "फ़ाइल सफलतापूर्वक सहेजी गई!", Toast.LENGTH_SHORT).show()
} catch (e: Exception) {
e.printStackTrace()
Toast.makeText(this, "फ़ाइल सहेजने में त्रुटि: ${e.message}", Toast.LENGTH_SHORT).show()
}
}
}
चरण 4 - AndroidManifest.xml (स्टोरेज अनुमति - आंतरिक स्टोरेज के लिए आवश्यक नहीं)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.textfilewrite">
<!-- आंतरिक स्टोरेज के लिए WRITE_EXTERNAL_STORAGE अनुमति की आवश्यकता नहीं -->
<application ...>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ऐप चलाएं
टेक्स्ट दर्ज करें और "फ़ाइल सहेजें" बटन दबाएं। फ़ाइल ऐप के आंतरिक स्टोरेज में /data/data/com.example.textfilewrite/files/mytextfile.txt पर सहेजी जाएगी।
नोट: यह आंतरिक स्टोरेज (ऐप-निजी) में लिखता है। बाहरी स्टोरेज (साझा) में लिखने के लिएWRITE_EXTERNAL_STORAGEअनुमति औरContext.getExternalFilesDir()याMediaStoreAPI (Android 10+) की आवश्यकता होती है।