android字节流保存,android数据存储之文件存储方法

本文介绍了Android中文件存储的基本方法,包括使用字节流进行文件读写,以及如何在SD卡上进行文件操作。通过openFileOutput和openFileInput在内部存储中保存和读取文件,同时展示了如何使用SD卡助手类在外部存储进行读写。文章还包含了AndroidManifest.xml中添加读写SD卡权限的注意事项。

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

文件存储是 Android 中最基本的一种数据存储方式,它不对存储的内容进行任何的格式化处理,所有数据都是原封不动的保存到文件当中的。

概述

文件存取的核心就是输入流和输出流。

Android文件的操作模式

bed9dec735b1fe42e4a511ea8655c48e.png

文件的相关操作方法

5d5df93e26d6a6755bfb57936a7bc344.png

文件读写的实现

openFileOutput和openFileInput方法

/**

* openFIleOutput ,openFileInput

* 这两种方法同sp一样只能讲文件保存到手机内存固定的路径中,

* 默认为 /data/data//files

*/

private void save2File() {

try {

//向文件写入内容

FileOutputStream os = openFileOutput("file.txt", Context.MODE_PRIVATE);

String text = "写数据到文件";

os.write(text.getBytes("utf-8"));

//关闭流

os.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

*

*/

private void readFile() {

try {

FileInputStream ins = openFileInput("file.txt");

byte[] buffer = new byte[100];

int byteCount = ins.read(buffer);

String text = new String(buffer,0,byteCount,"utf-8");

Toast.makeText(this,text,Toast.LENGTH_SHORT).show();

ins.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

文件存储位置

/data/data//files目录下

openFileOutput和openFileInput方法可以获得操作文件的OutputStream以及InputStream对象,而且可以通过流对象处理任何文件的数据,但是这两个方法同SharedPreferences一样,只能在手机内存卡的指定目录建立文件,因此在使用上仍然有一定的局限性。

读取SD卡上的文件

5fe563498eab51522396b8e5d94a8465.png

main_activity.xml:

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

android:id="@+id/LinearLayout1"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context="com.jay.example.filedemo2.MainActivity">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="清输入文件名" />

android:id="@+id/edittitle"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="文件名" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="清输入文件内容" />

android:id="@+id/editdetail"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="文件内容" />

android:id="@+id/btnsave"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="保存到SD卡" />

android:id="@+id/btnclean"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="清空" />

android:id="@+id/btnread"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="读取sd卡中的文件" />

接着我们来写一个SD操作类: SDFileHelper.Java

public class SDFileHelper {

private Context context;

public SDFileHelper() {

}

public SDFileHelper(Context context) {

super();

this.context = context;

}

//往SD卡写入文件的方法

public void savaFileToSD(String filename, String filecontent) throws Exception {

//如果手机已插入sd卡,且app具有读写sd卡的权限

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

filename = Environment.getExternalStorageDirectory().getCanonicalPath() + "/" + filename;

//这里就不要用openFileOutput了,那个是往手机内存中写数据的

FileOutputStream output = new FileOutputStream(filename);

output.write(filecontent.getBytes());

//将String字符串以字节流的形式写入到输出流中

output.close();

//关闭输出流

} else Toast.makeText(context, "SD卡不存在或者不可读写", Toast.LENGTH_SHORT).show();

}

//读取SD卡中文件的方法

//定义读取文件的方法:

public String readFromSD(String filename) throws IOException {

StringBuilder sb = new StringBuilder("");

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

filename = Environment.getExternalStorageDirectory().getCanonicalPath() + "/" + filename;

//打开文件输入流

FileInputStream input = new FileInputStream(filename);

byte[] temp = new byte[1024];

int len = 0;

//读取文件内容:

while ((len = input.read(temp)) > 0) {

sb.append(new String(temp, 0, len));

}

//关闭输入流

input.close();

}

return sb.toString();

}

}

接着MainActivity.java实现相关逻辑:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

private EditText editname;

private EditText editdetail;

private Button btnsave;

private Button btnclean;

private Button btnread;

private Context mContext;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mContext = getApplicationContext();

bindViews();

}

private void bindViews() {

editname = (EditText) findViewById(R.id.edittitle);

editdetail = (EditText) findViewById(R.id.editdetail);

btnsave = (Button) findViewById(R.id.btnsave);

btnclean = (Button) findViewById(R.id.btnclean);

btnread = (Button) findViewById(R.id.btnread);

btnsave.setOnClickListener(this);

btnclean.setOnClickListener(this);

btnread.setOnClickListener(t

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值