android 本地文件读写,Android开发存储方式详解之本地文件读写实例

1、布局文件:输入框、按钮和文本

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:id="@+id/ed_text"

android:layout_width="match_parent"

android:layout_height="300dp"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:ems="10" >

android:id="@+id/write"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/ed_text"

android:layout_centerHorizontal="true"

android:text="写入并读取" />

android:id="@+id/txt_view"

android:layout_width="match_parent"

android:layout_height="200dp"

android:layout_alignParentLeft="true"

android:layout_below="@+id/write"

android:text="TextView" />

2、使用代码实现在输入框中输入文字,点击按钮将输入框中的文字写入本地文件后,再读取该文件并将内容显示在文本上。

import java.io.ByteArrayOutputStream;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import android.annotation.SuppressLint;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

public class MainActivity extends Activity {

private EditText edit;

private Button button;

private TextView textView;

@SuppressLint("SdCardPath")

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

edit= (EditText) findViewById(R.id.ed_text);

button = (Button) findViewById(R.id.write);

textView = (TextView) findViewById(R.id.txt_view);

button.setOnClickListener(new OnClickListener(){

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

WriteFiles(edit.getText().toString());

textView.setText(ReadFiles());

}

});

}

//保存文件内容

public void WriteFiles(String content){

try {

//文件不存在是自动创建,默认写入/deta/data//files下

FileOutputStream fos = openFileOutput("a.txt", MODE_PRIVATE);

fos.write(content.getBytes());

fos.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

//读取文件内容

public String ReadFiles(){

String content = null;

try {

FileInputStream fis = openFileInput("a.txt");

ByteArrayOutputStream baos = new ByteArrayOutputStream();

//每次读取1024字节

byte[] buffer = new byte[1024];

int len = 0;

while ((len=fis.read(buffer)) != -1) {

//每次写一个buffer,并且从0开始,全部写出去

baos.write(buffer, 0, len);

}

content = baos.toString();

fis.close();

baos.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return content;

}

}需要权限:

源码下载

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值