Android内部存储示例

Android内部存储示例

我们能够保存或读取设备的内部存储空间中的数据。FileInputStream和FileOutputStream类用于将数据读写到文件中。

在这里,我们将要读写数据到设备的内部存储空间。

读写数据到Android内部存储的示例

activity_main.xml

从工具栏中拖动2个EditText、2个TextView和2个Button,现在activity_main.xml文件将如下所示:

<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"  
    tools:context=".MainActivity" >  

    <EditText  
        android:id="@+id/editText1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentRight="true"  
        android:layout_alignParentTop="true"  
        android:layout_marginRight="20dp"  
        android:layout_marginTop="24dp"  
        android:ems="10" >  

        <requestFocus />  
    </EditText>  

    <EditText  
        android:id="@+id/editText2"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignRight="@+id/editText1"  
        android:layout_below="@+id/editText1"  
        android:layout_marginTop="24dp"  
        android:ems="10" />  

    <TextView  
        android:id="@+id/textView1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignBaseline="@+id/editText1"  
        android:layout_alignBottom="@+id/editText1"  
        android:layout_alignParentLeft="true"  
        android:text="File Name:" />  

    <TextView  
        android:id="@+id/textView2"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignBaseline="@+id/editText2"  
        android:layout_alignBottom="@+id/editText2"  
        android:layout_alignParentLeft="true"  
        android:text="Data:" />  

    <Button  
        android:id="@+id/button1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignLeft="@+id/editText2"  
        android:layout_below="@+id/editText2"  
        android:layout_marginLeft="70dp"  
        android:layout_marginTop="16dp"  
        android:text="save" />  

    <Button  
        android:id="@+id/button2"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignBaseline="@+id/button1"  
        android:layout_alignBottom="@+id/button1"  
        android:layout_toRightOf="@+id/button1"  
        android:text="read" />  

</RelativeLayout>

Activity 类

让我们编写代码,从内部存储器中读取和写入数据。

“`java
package example.javatpoint.com.internalstorage;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {
EditText editTextFileName,editTextData;
Button saveButton,readButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

<pre><code> editTextFileName=findViewById(R.id.editText1);
editTextData=findViewById(R.id.editText2);
saveButton=findViewById(R.id.button1);
readButton=findViewById(R.id.button2);

//Performing Action on Read Button
saveButton.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View arg0) {
String filename=editTextFileName.getText().toString();
String data=editTextData.getText().toString();

FileOutputStream fos;
try {
fos = openFileOutput(filename, Context.MODE_PRIVATE);
//default mode is PRIVATE, can be APPEND etc.
fos.write(data.getBytes());
fos.close();

Toast.makeText(getApplicationContext(),filename + " saved",
Toast.LENGTH_LONG).show();

} catch (FileNotFoundException e) {e.printStackTrace();}
catch (IOException e) {e.printStackTrace();}

}

});

//Performing Action on Read Button
readButton.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View arg0) {
String filename=editTextFileName.getText().toString();
StringBuffer stringBuffer = new StringBuffer();
try {
//Attaching BufferedReader to the FileInputStream by the help of InputStreamReader
BufferedReader inputReader = new BufferedReader(new InputStreamReader(
openFileInput(filename)));
String inputString;
//Reading data line by line and storing it into the stringbuffer
while ((inputString = inputReader.readLine()) != null) {
stringBuffer.append(inputString + "\n");
}

} catch (IOException e) {
e.printStackTrace();
}
//Displaying data on the toast
Toast.makeText(getApplicationContext(),stringBuffer.toString(),Toast.LENGTH_LONG).show();

}

});
}
</code></pre>

}
“““

输出:

Android内部存储示例

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程