android 存储文本,Android数据存储系列————文本存储

本文详细介绍了Android应用中如何进行文件读写操作。通过`FileOutputStream`和`FileInputStream`实现文本内容的保存和读取,展示了在`onCreate()`方法中设置监听器,并在点击事件中调用读写文件的方法。代码中包含了异常处理以确保资源的正确关闭。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.hacker.filestream;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

import java.io.ByteArrayOutputStream;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;public classMainActivity extends AppCompatActivity {//定义文件夹名

private String fileName="save.txt";//文件输出流

privateFileOutputStream fileOutputStream;//字节输出流

ByteArrayOutputStream stream;//文件输入流

privateFileInputStream fileInputStream;//输入框

privateEditText inputFileText;//保存按钮,读取按钮

privateButton saveButton,readButton;//输入内容

privateTextView outFileText;//输入框的内容

privateString fileText;

@Overrideprotected voidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

inputFileText=findViewById(R.id.edit_textinput);

saveButton=findViewById(R.id.button_savetext);

readButton=findViewById(R.id.button_readtext);

outFileText=findViewById(R.id.text_textoutput);

saveButton.setOnClickListener(newView.OnClickListener() {

@Overridepublic voidonClick(View v) {

WriteFile();

}

});

readButton.setOnClickListener(newView.OnClickListener() {

@Overridepublic voidonClick(View v) {

ReadFile();

}

});

}//写入文件,使用输出流

private voidWriteFile(){try{//私有模式创建文件

fileOutputStream=openFileOutput(fileName,MODE_PRIVATE);//获取输入框输入的内容

fileText=inputFileText.getText().toString();//写入数据

fileOutputStream.write(fileText.getBytes());

fileOutputStream.flush();//关闭文件输出流

fileOutputStream.close();

}catch(FileNotFoundException e){

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{//在finally中关闭流!因为如果找不到数据就会异常,我们也需要对其进行关闭

try{if (fileOutputStream!=null){//这里判断的目的是因为如果没有找到数据的时候,两种流就不会实例化,既然没有实例化则如果调用就会提示null

fileOutputStream.close();

}

}catch(IOException e){

e.printStackTrace();

}

}

}//读取文件,使用输入流

private voidReadFile(){try{//获取指定文件的输入流

fileInputStream=openFileInput(fileName);//实例化字节输出流

stream=newByteArrayOutputStream();//输入输出缓存

byte[] buffer=new byte[1024];int length=-1;//读取文件输入流的内容到缓存中

while((length=fileInputStream.read(buffer))!=-1){//将缓存写入输入流

stream.write(buffer,0,length);

}//关闭输出流

stream.close();//关闭文件输入流

fileInputStream.close();//读取文本内容

outFileText.setText(stream.toString());

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();//无论是否异常都要关闭输入输出流

}finally{try{//输出流不为空,则关闭输出流

if (stream!=null){

stream.close();//输入流不为空,则关闭输入流

}else if(fileInputStream!=null){

fileInputStream.close();

}

}catch(IOException e) {

e.printStackTrace();

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值