1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AndroidStudio(kotlin)テキスト文字列を内部ストレージに保存&読み出し

Posted at

やりたいこと

  • 「保存する」ボタンと「読み出す」ボタンを設置する。
  • テキストの入力フォームを設置する。
  • 入力フォームから文字を入力して「保存する」を押すと、アプリの内部ストレージに文字が保存される。
  • 「読み出す」ボタンを押すと、内部ストレージに保存した文字列を読み出して、ビューに表示する。

画面

グリーンの領域に文字列を入力して「保存する」を押した後、「読み出す」を押すと、ピンクの領域にその文字列が表示される。

image.png

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/saveBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginLeft="32dp"
        android:layout_marginEnd="32dp"
        android:layout_marginRight="32dp"
        android:layout_marginBottom="32dp"
        android:text="保存する"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/readBtn"
        app:layout_constraintHorizontal_bias="0.029"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/readBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="32dp"
        android:layout_marginRight="32dp"
        android:layout_marginBottom="32dp"
        android:text="読み出す"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <EditText
        android:id="@+id/inputContents"
        android:layout_width="339dp"
        android:layout_height="109dp"
        android:layout_marginTop="32dp"
        android:background="#D6ECC7"
        android:ems="10"
        android:gravity="start|top"
        android:inputType="textMultiLine"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/displayContents"
        android:layout_width="338dp"
        android:layout_height="298dp"
        android:background="#46F235F8"
        android:text="ここに表示される。"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/inputContents" />
</androidx.constraintlayout.widget.ConstraintLayout>

メイン処理


package com.example.yamato200610c

import android.content.Context
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import java.io.BufferedReader
import java.io.File

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val fileName = "contents.txt"

        // 保存ボタン押下時の挙動。
        saveBtn.setOnClickListener(View.OnClickListener {
            val contents = inputContents.text
            saveFile(fileName, contents.toString())
        })

        // 読み出すボタン押下時の挙動。
        readBtn.setOnClickListener(View.OnClickListener {
            val buf: BufferedReader = readFile(fileName)
            val result = buf.use{it.readText()}
            displayContents.text = result
        })
    }

    // 保存処理。
    private fun saveFile(file: String, str: String) {
        applicationContext.openFileOutput(file, Context.MODE_PRIVATE).use {
            it.write(str.toByteArray())
        }
    }

    // 読み出し処理。
    private fun readFile(file: String): BufferedReader {
        val readFile = File(applicationContext.filesDir, file)
        return readFile.bufferedReader()
    }
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?