কম্পিউটার টিউটোরিয়াল

Kotlin-এ Android-এ টেক্সট ফাইল তৈরি করে ডেটা সংরক্ষণ করার পদ্ধতি

পূর্ব প্রস্তুতি

এই উদাহরণটি দেখায় কিভাবে আંતরিক স্টোরেজে (Internal Storage) একটি টেক্সট ফাইল তৈরি করে EditText-এ ইনপুট করা ডেটা লেখা যায়।

ধাপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio-তে File → New Project নির্বাচন করুন।

ধাপ ২: 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">
        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        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="Save File"
        android:textColor="@android:color/background_dark"
        android:textStyle="bold" />

</RelativeLayout>

ধাপ ৩: 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, "File saved successfully!", Toast.LENGTH_SHORT).show()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}

ধাপ ৪: AndroidManifest.xml (স্টোরেজ পারমিশন)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.textfilesave">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <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>

নোট

  • `openFileOutput` আন্তরিক স্টোরেজে ফাইল তৈরি করে (অন্য অ্যাপ অ্যাক্সেস করতে পারে না)।
  • এক্সটার্নাল স্টোরেজে লেখার জন্য `WRITE_EXTERNAL_STORAGE` পারমিশন এবং `getExternalFilesDir()` ব্যবহার করুন।
  • Android 10+ (API 29+) এ স্কোপড স্টোরেজ নিয়ম মেনে চলতে হয়।

পরীক্ষা করুন

টেক্সট লিখুন, "Save File" বাটনে ক্লিক করুন। সফল হলে টোস্ট মেসেজ দেখাবে।

  1. অ্যান্ড্রয়েডে টেক্সট ফাইল তৈরি করে ডেটা লিখার পদ্ধতি

  2. অ্যান্ড্রয়েডে কাস্টম নোটিফিকেশন লেআউট ও টেক্সট কালার তৈরি করা

  3. অ্যান্ড্রয়েড অ্যাপে সিম্পল টেক্সট ফাইল পড়ার পদ্ধতি

  4. Android-এ XML ফাইল ব্যবহার করে অ্যানিমেশন তৈরির পদ্ধতি