Write and read from Internal Storage

Description

The following code shows how to Write and read from Internal Storage.

Example

Main layout xml file


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/internal_storage_label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10px"
        android:text="Enter some text to write on the internal storage, then read back:" />

    <EditText
        android:id="@+id/internal_storage_input"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10px" />

    <Button
        android:id="@+id/internal_storage_write_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Write" />

    <Button
        android:id="@+id/internal_storage_read_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Read" />

    <TextView
        android:id="@+id/internal_storage_output"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10px" />


</LinearLayout>

Main Activity Java code


//from  www. j  a  va 2 s .  c  om
package com.java2s.myapplication3.app;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class MainActivity extends Activity {
    private static final String LINE_SEP = System.getProperty("line.separator");

    private EditText input;
    private TextView output;
    private Button write;
    private Button read;

    @Override
    public void onCreate(final Bundle icicle) {
        super.onCreate(icicle);
        this.setContentView(R.layout.activity_main);

        this.input = (EditText) findViewById(R.id.internal_storage_input);
        this.output = (TextView) findViewById(R.id.internal_storage_output);

        this.write = (Button) findViewById(R.id.internal_storage_write_button);
        this.write.setOnClickListener(new OnClickListener() {
            public void onClick(final View v) {
                write();
            }
        });

        this.read = (Button) findViewById(R.id.internal_storage_read_button);
        this.read.setOnClickListener(new OnClickListener() {
            public void onClick(final View v) {
                read();
            }
        });
    }

    private void write() {
        FileOutputStream fos = null;
        try {
            // note that there are many modes you can use
            fos = openFileOutput("test.txt", Context.MODE_PRIVATE);
            fos.write(input.getText().toString().getBytes());
            Toast.makeText(this, "File written", Toast.LENGTH_SHORT).show();
            input.setText("");
            output.setText("");
        } catch (FileNotFoundException e) {
            Log.e("java2s.com", "File not found", e);
        } catch (IOException e) {
            Log.e("java2s.com", "IO problem", e);
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                // ignore, and take the verbosity punch from Java ;)
            }
        }
    }

    private void read() {
        FileInputStream fis = null;
        Scanner scanner = null;
        StringBuilder sb = new StringBuilder();
        try {
            fis = openFileInput("test.txt");
            // scanner does mean one more object, but it's easier to work with
            scanner = new Scanner(fis);
            while (scanner.hasNextLine()) {
                sb.append(scanner.nextLine() + LINE_SEP);
            }
            Toast.makeText(this, "File read", Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            Log.e("java2s.com", "File not found", e);
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    // ignore, and take the verbosity punch from Java ;)
                }
            }
            if (scanner != null) {
                scanner.close();
            }
        }
        output.setText(sb.toString());
    }
}
Write and read from Internal Storage




















Home »
  Android »
    Android Basics »




Hello Android
Resources
Activity
Calendar
Camera
Contact
Content Provider
Database
Hardware
Intent
Location
Media
Network
Notification
Preference
Sensor
Service
SMS