本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。
原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/
SharedPreferences对象能够让你去保存一些“键值对”类型的数据,比如用户id,生日,性别,身份证号等等。但是,有的时候你需要去使用传统的文件系统去保存数据。例如你可能想要去保存一篇文章,而这篇文章要被展示在你的应用中。在Android系统中,你也可以使用java.io包去实现这个功能。
在Android系统中,第一种保存文件的方法是存储到内部设备。下面展示如何保存用书输入的字符串到内部存储设备。
1. 创建一个工程,Files。
2. main.xml中的代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Please enter some text" />
<EditText
android:id="@+id/txtText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnSave"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickSave"
android:text="Save" />
<Button
android:id="@+id/btnLoad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickLoad"
android:text="Load" />
</LinearLayout>
3. FilesActivity.java中的代码。
package net.manoel.Files;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import net.learn2develop.Files.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class FilesActivity extends Activity {
EditText textBox;
static final int READ_BLOCK_SIZE = 100;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textBox = (EditText) findViewById(R.id.txtText1);
}
public void onClickSave(View view) {
String str = textBox.getText().toString();
try
{
FileOutputStream fOut =
openFileOutput("textfile.txt",
MODE_WORLD_READABLE);
OutputStreamWriter osw = new
OutputStreamWriter(fOut);
//---write the string to the file---
osw.write(str);
osw.flush();
osw.close();
//---display file saved message---
Toast.makeText(getBaseContext(),
"File saved successfully!",
Toast.LENGTH_SHORT).show();
//---clears the EditText---
textBox.setText("");
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
public void onClickLoad(View view) {
try
{
FileInputStream fIn =
openFileInput("textfile.txt");
InputStreamReader isr = new
InputStreamReader(fIn);
char[] inputBuffer = new char[READ_BLOCK_SIZE];
String s = "";
int charRead;
while ((charRead = isr.read(inputBuffer))>0)
{
//---convert the chars to a String---
String readString =
String.copyValueOf(inputBuffer, 0,
charRead);
s += readString;
inputBuffer = new char[READ_BLOCK_SIZE];
}
//---set the EditText to the text that has been
// read---
textBox.setText(s);
Toast.makeText(getBaseContext(),
"File loaded successfully!",
Toast.LENGTH_SHORT).show();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
4. 在模拟器上面调试。
5. 输入一些字符串,然后按Save按钮。
6. 如果保存成功,就是弹出“File saved successfully! ”字样。EditText中的字符串同时被清空。
7. 按Load键,刚刚的字符串又会重新出现,这也证明了刚刚的字符串被成功的保存到文件系统中了。
分析:
想要把字符串保存到一个文件里面,需要使用FileOutputStream这个类。openFileOutput()这个方法打开了一个可供写入的文件,同时也指定了相应的模式。在这个例子中,使用了MODE_WORLD_READABLE常量,意味着这个文件可以被其他的应用读。
FileOutputStream fOut =
openFileOutput("textfile.txt",
MODE_WORLD_READABLE);
除了MODE_WORLD_READABLE常量,还有MODE_PRIVATE,MODE_APPEND,MODE_WORLD_WRITEABLE。
想要把字符串流转换成字节流,需要使用一个OutputStreamWriter类,初始化的时候需要传入一个FileOutputStream对象:
OutputStreamWriter osw = new
OutputStreamWriter(fOut);
调用write()接口去往文件里面写字符串,想要确保所有的字节都被写入的文件,需要调用flush()接口。最后,调用close()接口去关闭流。
//---write the string to the file---
osw.write(str);
osw.flush();
osw.close();
上面是写入的过程,读取的过程与之类似,我就不一一赘述了。大家请参考以下代码。
public void onClickLoad(View view) {
try
{
FileInputStream fIn =
openFileInput("textfile.txt");
InputStreamReader isr = new
InputStreamReader(fIn);
char[] inputBuffer = new char[READ_BLOCK_SIZE];
String s = "";
int charRead;
while ((charRead = isr.read(inputBuffer))>0)
{
//---convert the chars to a String---
String readString =
String.copyValueOf(inputBuffer, 0,
charRead);
s += readString;
inputBuffer = new char[READ_BLOCK_SIZE];
}
//---set the EditText to the text that has been
// read---
textBox.setText(s);
Toast.makeText(getBaseContext(),
"File loaded successfully!",
Toast.LENGTH_SHORT).show();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
最后,我们用DDMS工具去查看一下文件是否被成功地保存到内部存储设备上。
可以看到,文件被保存到data/data/net.manoel.Files/files路径下面。
分享到:
相关推荐
Android 3种数据保存(SharedPreferences存储 内部文件存储 数据库存储),如项目包有问题请联系我谢谢
新版Android开发教程&笔记--基础入门一.pdf 新版Android开发教程&笔记--基础入门二.pdf 新版Android开发教程&笔记三--环境搭建与解析.pdf 新版Android开发教程&笔记四--Dalvik ADB.pdf 新版Android开发教程+笔记五--...
Android开发教程笔记完全版 pdf.我看了一下,自己感觉可以.
[14本经典Android开发教程]-3-Android SDK 中文开发文档 什么是 Android? Android 是一个专门针对移动设备的软件集,它包括一个操作系统,中间件和一些重要的应用程序。Beta版的 Android SDK 提供了在Android 平台上...
Android开发教程+笔记 Android开发教程+笔记 Android开发教程+笔记 Android开发教程+笔记 Android开发教程+笔记 Android开发教程+笔记 Android开发教程+笔记 Android开发教程+笔记 Android开发教程+笔记 Android开发...
Android Android Android Android 开发背景 � 计算技术、无线接入技术的发展,使嵌入式系统逐渐有能力对桌面系统常规业务进行支持。 � 谷歌长期以来奉行的移动发展战略:通过与全球各地的手机制造商和移动运营商...
新版Android开发教程&笔记1--基础入门一.pdf 新版Android开发教程&笔记2--基础入门二.pdf 新版Android开发教程&笔记3--环境搭建与解析.pdf 新版Android开发教程&笔记4--Dalvik ADB.pdf 新版Android开发教程+笔记5--...
[14本经典Android开发教程] 9 Android2 3用户...[14本经典Android开发教程] 1 Android开发从入门到精通 http: download csdn net detail cleopard 8355245 [14本经典Android开发教程] 2 Android开发手册 API函数详解 ...
android 内部存储 sd卡app私有文件 等
[14本经典Android开发教程]-11-Android系统...[14本经典Android开发教程]-1-Android开发从入门到精通 http://download.csdn.net/detail/cleopard/8355245 [14本经典Android开发教程]-2-Android开发手册—API函数详解 ...
Android NFC 开发教程 NFC手机推出后,基于NFC的Android开发升温,银行卡、公交卡充值、余额查询等业务不断推出,开发NFC功能的Android程序也成为程序猿们的工作。
[14本经典Android开发教程]-4-Android应用...[14本经典Android开发教程]-1-Android开发从入门到精通 http://download.csdn.net/detail/cleopard/8355245 [14本经典Android开发教程]-2-Android开发手册—API函数详解 ...
针对Android中的文件存储方式,本程序以简练的方式,实现了文件内部存储和SDCard存储。代码清晰,注解详细,方便学习。
《Android开发案例驱动教程》 配套代码。 注: 由于第12,13,14章代码太大,无法上传到一个包中。 这三节代码会放到其他压缩包中。 作者:关东升,赵志荣 Java或C++程序员转变成为Android程序员 采用案例驱动模式...
DownLoadImageToGallery: Android 下载图片保存到相册
14本安卓android开发教程书籍源代码让您从安卓开发新手到高手,快速具备独立开发经验,最全api教程大合集 安卓AndroidAPI教程 安卓android编译总结 安卓android开发从零开始代码若水 二维码生成图片 安卓中国象棋源码 ...
android 视频录制 并且保存到本地 注意 保存到sd卡 必须 自己手动创建文件夹 PathUtil 类 dir 的参数 自己改
根据调用系统广播实现,先保存到本地文件管理中,完美运行,可以根据后台返回的String类型图片地址来保存 给个好评噢~
【第一版第九章】老罗Android开发视频--存储数据和文件(7集) 【第一版第十章】老罗Android开发视频--对话框介绍(4集) 【第一版第十一章】老罗Android开发视频--通知的使用(2集) 【第一版第十二章】老罗...