2007 杨昌荣 11:48:54
第一行代码Android(第2版) 文件储存
1.打开软件创建项目工程。
2.找到activity_main.xml和MainActivit.java输入正确代码。
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:layout_margin="3dp"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="数据的内部存储"
android:textSize="22sp" />
<Button
android:id="@+id/but_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="存储数据"
android:textSize="22sp"/>
<Button
android:id="@+id/but_read"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取数据"
android:textSize="22sp"/>
</LinearLayout>
2007 杨昌荣 11:49:25
MainActivit.java:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201214113429831.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl81MzA2NTQzMg==,size_16,color_FFFFFF,t_70#pic_center)
```java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
/**
* 主界面的逻辑代码
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button but_save,but_read;//保持与读取按钮
private Object FileInputStream;
private String fileName = "mydb.text";//文件名称
// String strNr = "HelloWorld";//要保存的数据
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but_save = findViewById(R.id.but_save);
but_read = findViewById(R.id.but_read);
but_save.setOnClickListener(this);
but_read.setOnClickListener(this);
}
//点击处理方法
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.but_save://保存数据
savaData();
break;
case R.id.but_read:
readData();
break;
}
}
//保存数据的方法
private void savaData() {
// String fileName = "mydb.text";//文件名称
String strNr = "HelloWorld,I'm coming";//要保存的数据
FileOutputStream fos = null;
try {
fos = openFileOutput(fileName,MODE_PRIVATE);
fos.write(strNr.getBytes());//将数据写入文件中去
Toast.makeText(this, "保存数据成功", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (fos!=null){
fos.close();
}
}catch (Exception ex){
ex.printStackTrace();
}
}
}
//读取数据的方法
private void readData() {
String strNr = "";//要读取的内容变量
// FileInputStream = null;
InputStream fis = null;
try {
fis = openFileInput(fileName);//获取输入流对象
byte[] buffer = new byte[fis.available()];//创建读取的缓冲对象
fis.read(buffer); //将文件内容读取到缓冲区中去
strNr = new String(buffer); //转换成字符串
Toast.makeText(this, strNr, Toast.LENGTH_SHORT).show();//显示
}catch (Exception ex){
ex.printStackTrace();
}finally {
try {
if (fis!=null) {
} else {
fis.close();
}
}catch (Exception ex){
ex.printStackTrace();
}
}
}
}
3.点击运行输出如下图。