简体   繁体   中英

Android Studio: Storing/Reading a Text File in a Project

I know how to read from a text file under other languages such as Visual Basic and have learned how to in Java as well, but in Android Studio how would I accomplish this? Also, when working with Visual Basic and Java I always accessed the Text File form the computer, but in Android Studio is there a way to include the Text File in the project so that when the app is downloaded it comes with the text file? Furthermore, is the text file method the way to go when storing data or is their other methods? Thank you in advance. :)

You can look at the following code to read or write in a file in android.I have written the my code in android studio;

Here is my XML file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:background="#008080"
        android:padding="5dp"
        android:text="Android File example"
        android:textColor="#fff" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="22dp"
        android:ems="10"
        android:layout_margin="5dp">
        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/editText1"
        android:text="Write Text into File"
        android:onClick="WriteBtn"
        android:layout_margin="5dp"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/button1"
        android:text="Read Text From file"
        android:onClick="ReadBtn"
        android:layout_margin="5dp" />
</RelativeLayout>

Here is my Java File:

package com.example.sudiproy.fileexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class MainActivity extends AppCompatActivity {

    EditText textmsg;
    static final int READ_BLOCK_SIZE = 100;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textmsg= (EditText) findViewById(R.id.editText1);

    }

    public void WriteBtn(){
        try{
            FileOutputStream fileOut=openFileOutput("myText.txt",MODE_PRIVATE);
            OutputStreamWriter outputWriter=new OutputStreamWriter(fileOut);
            outputWriter.write(textmsg.getText().toString());
            outputWriter.close();

            Toast.makeText(getBaseContext(), "File saved successfully!",
                    Toast.LENGTH_SHORT).show();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

    public void ReadBtn(){
        try{
            FileInputStream fileIn=openFileInput("myText.txt");
            InputStreamReader InputRead=new InputStreamReader(fileIn);
            char[] inputBuffer=new char[READ_BLOCK_SIZE];
            String s="";
            int charRead = 0;

            while ((charRead=InputRead.read(inputBuffer))>0) {
                // char to string conversion
                String readstring=String.copyValueOf(inputBuffer,0,charRead);
                s +=readstring;
            }
            InputRead.close();
            Toast.makeText(getBaseContext(), s,Toast.LENGTH_SHORT).show();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

Please do not forget to include the following permissions in your manifest:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM