首页 > 系统 > Android > 正文

Android进阶之路 - 四大存储方式之文件存储

2019-11-09 14:38:02
字体:
来源:转载
供稿:网友

说到Android中的存储方式,大家应该都会想到手机的文件存储,sd卡存储,sp存储与数据库存储,这些其实我之前已经写过一次,但是当时编辑的比较凌乱,看起来不太方便,所以现在重新写四大存储方式并进行讲解,此本讲的就是文件的存储与读取了,闲话少说,Code送上。

UI Map: 这里写图片描述 MainActivity Code:

package com.example.storagedispose;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import android.os.Bundle;import android.app.Activity;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 implements OnClickListener { PRivate EditText mEdit; private TextView mContent; private FileOutputStream openFileOutput; private BufferedWriter bufferedWriter; private FileInputStream openFileInput; private BufferedReader bufferedReader; private StringBuilder builderContent; private String data; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { mContent = (TextView) findViewById(R.id.main_content); mEdit = (EditText) findViewById(R.id.main_edit); Button mSave = (Button) findViewById(R.id.save_one); Button mLoad = (Button) findViewById(R.id.load_one); mSave.setOnClickListener(this); mLoad.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.save_one: data = mEdit.getText().toString(); onsave(data); break; case R.id.load_one: onload(); break; default: break; } } // 进行文件读取,直接抛出的总异常 private void onload() { // 读取手机文件名为--save_one的文件 try { openFileInput = openFileInput("save_one"); // 字符读取流 bufferedReader = new BufferedReader(new InputStreamReader( openFileInput)); // 创建StringBuilder用于存储数据 builderContent = new StringBuilder(); // 查看是否有数据,有的话直接添加到我们的容器中 String line = null; while ((line = bufferedReader.readLine()) != null) { builderContent.append(line); mContent.setText(builderContent); } } catch (IOException e) { e.printStackTrace(); } finally { if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } // 文件的存儲 private void onsave(String data) { // 将数据存储在本地文件中 try { // 将数据存储于指定的文件-第一个参数为文件名,第二个参数确定写入方式 openFileOutput = openFileOutput("save_one", MODE_PRIVATE); // 创建字符输入流,吧上方的文件内容写进去 bufferedWriter = new BufferedWriter(new OutputStreamWriter( openFileOutput)); // 将我们输入的数据保存到这个文件夹里面 bufferedWriter.write(data); } catch (IOException e) { e.printStackTrace(); } finally { // 这里的执行完全是为了避免内存的泄漏 if (bufferedWriter != null) { try { bufferedWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } }}

MainActivity xml Code:

<LinearLayout 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" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/main_edit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="在此输入要保存的信息" /> <Button android:id="@+id/save_one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="文件保存" /> </LinearLayout> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/load_one" android:text="读取文件的数据" android:gravity="center" /> <TextView android:id="@+id/main_content" android:text="读取数据展示区" android:gravity="center" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp" /></LinearLayout>

end map: 1.这里我们输入要保存的东西,然后点击保存 这里写图片描述 2.通过DDMS导出数据,进行数据查询 这里写图片描述 3.查看导出的数据(我们发现数据是没有问题的哦,在此写入成功) 这里写图片描述 4.我们现在使用的就是数据读取了(显示区域也是没有问题) 这里写图片描述

使用方式: 1.使用openFileOutput存储,openFileInput读取 2.输入的时候为写入,所以使用的是Output,输出的时候为读取,使用的Input 3.对应的使用BufferedWrite与BufferedRead,之后传入上文的FileOutputStream与FileInputStream 4.BufferedRead因为要读取,所有记得StringBuilder的容器 5.记得close,避免内存泄漏

注意:

记得添加读写的权限

<uses-permission android:name="android.permission.WRITE_PROFILE"/> <uses-permission android:name="android.permission.READ_PROFILE"/>

文件存储有时候往往不能满足我们的需求,因为他只有一个文本信息,个人认为更多的就是一个文本展示,如一些用户的使用说明等,下篇为大家带来Sp的存储与读取,可通过以下链接进行跳转查看。

sp存储方式:http://blog.csdn.net/QQ_20451879/article/details/54983207


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表