Skip to content

Instantly share code, notes, and snippets.

@amoozeshbebin
Created November 21, 2023 11:12
Show Gist options
  • Save amoozeshbebin/3a8dea6bb396bf63944309a1929576d2 to your computer and use it in GitHub Desktop.
Save amoozeshbebin/3a8dea6bb396bf63944309a1929576d2 to your computer and use it in GitHub Desktop.
File Permission
MainActivity.kt:
package com.example.filepermission
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import java.io.BufferedReader
import java.io.FileOutputStream
import java.io.InputStreamReader
import java.lang.Exception
class MainActivity : AppCompatActivity() {
private lateinit var fileName:EditText
private lateinit var fileContent:EditText
private lateinit var btnSave:Button
private lateinit var btnLoad:Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
init()
}
fun init(){
bindViews()
btnSave.setOnClickListener {
try {
val name = fileName.text.toString()
val content = fileContent.text.toString()
val fileOutputStream = openFileOutput(name, MODE_PRIVATE)
fileOutputStream.write(content.toByteArray())
Toast.makeText(this,"File Saved",Toast.LENGTH_SHORT)
}catch (e:Exception){
println(e)
}
}
btnLoad.setOnClickListener {
try {
val name = fileName.text.toString()
val fileInputStream = openFileInput(name)
val inputStreamReader = InputStreamReader(fileInputStream)
val bufferedReader = BufferedReader(inputStreamReader)
val txt = bufferedReader.readText()
fileContent.setText(txt)
Toast.makeText(this,"File Loaded",Toast.LENGTH_SHORT)
}catch (e:Exception){
Toast.makeText(this,e.message,Toast.LENGTH_SHORT)
}
}
}
fun bindViews(){
fileName = findViewById(R.id.file_name)
fileContent = findViewById(R.id.file_content)
btnSave = findViewById(R.id.btn_save)
btnLoad = findViewById(R.id.btn_load)
}
}
_____________________________________________________________________
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp"
android:layout_marginTop="20dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="filename"
android:textSize="21sp"
android:layout_marginRight="10dp"/>
<EditText
android:id="@+id/file_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text"
android:text="file.txt" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="content"
android:textSize="21sp"
android:layout_marginRight="10dp"/>
<EditText
android:id="@+id/file_content"
android:layout_width="wrap_content"
android:layout_height="193dp"
android:layout_weight="1"
android:ems="10"
android:inputType="text" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal">
<Button
android:id="@+id/btn_save"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="save"
android:layout_margin="5dp"/>
<Button
android:id="@+id/btn_load"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="load"
android:layout_margin="5dp"/>
</LinearLayout>
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment