文件存储分为两种:
(1)内部存储: 存放路径为/data/data/package_name/files/
(2)外部存储: SD卡
(3)扩充对资源xml文件的操作
一、内部存储
1.两个函数
openFileOutput 写入 权限:MODE_PRIVATE, MODE_APPEND, MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE
openFileInput 读取
2.写文件
FileOutputStream fos = openFileOutput(FILENAME, FILE_ACCESS);
String sContent = "12 34 56 789";
//getBytes Returns a new byte array containing the characters of this string encoded using the system's default charset.
fos.write(sContent.getBytes());
fos.flush(); //小量数据时系统存在内存中,可多次write后再调一次flush刷新到flash
fos.close();
3.读文件
FileInputStream fis = openFileInput(FILENAME);
byte[] buffer = new byte[fis.available()]; //该接口不保证等于文件的实际长度
while (fis.read(buffer) != -1){
}
String str = new String(buffer);
fis.close();
注意:以上读写文件操作都是not save的,需要进行try/catch
二、SD卡存储
和文件存储类似,操作sdcard文件前提是
(1)要确认sdcard已挂载。
(2)以绝对路径方式创建文件
(3)检测sdcard文件存在及可读写性
在已挂在的外部USB设备上进行读写,sdcard具有可读写权限.
eg:
public String SDCARD_PATH = "/mnt/vold/usbdisk1/sda1";
public void createFileInSdcard(String _filename) {
String sTestContent = "testx.txt";
FileOutputStream fos = null;
File dir = null;
File sdcardFile = null;
dir = new File(SDCARD_PATH);
if (dir.exists() && dir.canWrite()) {
sdcardFile = new File(dir.getAbsolutePath() + "/" + _filename);
try {
sdcardFile.createNewFile();
if (sdcardFile.exists() && sdcardFile.canWrite()) {
fos = new FileOutputStream(sdcardFile);
//fos =openFileOutput(sdcardFile, Context.MODE_WORLD_WRITEABLE + Context.MODE_WORLD_READABLE);
fos.write(sTestContent.getBytes());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
【注】上面File.createNewFile方法创建的文件默认属性为rwx-rwx-rwx, 如果不想ugo都具有rwx权限,在检测sdcard存在后创建文件的操作
可以参照文件内部存储的方式创建。
在AVD上模拟SDcard方法
(1)通过android-sdk的tools下的mksdcard工具映射成本地的文件(模拟sdcard)
mksdcard -l sdcard 128M E:\android_proj\AVDSDcard
-l sdcard 定义sdcard的标签
128M 定义sdcard的容量
E:\android_proj\AVDSDcard 是本地的路径(文件)
(2)在运行配置里添加属性
![]()

(3)要在AVD里指定sdcard文件的路径
eclipse -> windows->Android Virtual Device Manager
![]()

(4)启动AVD,往sdcard里上传一个文件,如果能成功,则sdcard创建成功
![]()

(5)以上通过adb 可以往sdcard里上传文件,但是通过程序往里面写的时候会包no permission,需要在AndroidMainifest.xml里加上写权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
三、资源文件
程序开发常用的资源文件分两种:
原始资源文件:如音视频文件等 --->/res/raw 目录下
XML文件: --->/res/xml 目录下
1.解析raw文件
(1)获取资源对象Resources
(2)打开资源文件
(3)读取资源inputstream流,然后按自己的要求执行
(4)关闭资源文件
private void catResRawFile(int nResId){
InputStream sInStream = null;
Resources res = this.getResources();
byte[] bReadBuf = null;
sInStream = res.openRawResource(nResId);
try {
bReadBuf = new byte[sInStream.available()];
while (sInStream.read(bReadBuf) != -1) {
}
txtVContent.setText(new String(bReadBuf/*, "utf-8"*/));
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e(TAG, e.getMessage(), e);
e.printStackTrace();
} finally {
if (sInStream != null) {
try {
sInStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e(TAG, e.getMessage(), e);
e.printStackTrace();
}
}
}
}
2.解析XML文件
例:
<people>
<person name="李某某" age="21" height="1.81" />
<person name="王某某" age="25" height="1.76" />
<person name="张某某" age="20" height="1.69" />
</people>
(1)获取资源对象Resources
(2)获取xml资源的xml解析器XmlPullParser (Android平台标准的解析器)
(3)逐个节点解析
解法(一):
public void catResXmlFile(int nXmlId){
String sPeople = "";
String sName = "";
String sAge = "";
String sHeight = "";
String sContent = "";
String sAttrName = "";
String sAttrValue = "";
int nAttrNum = 0;
XmlPullParser xmlparser = this.getResources().getXml(nXmlId);
try {
while (xmlparser.next() != XmlPullParser.END_DOCUMENT) {
sPeople = xmlparser.getName();
if (sPeople != null && sPeople.equals("person")) {
nAttrNum = xmlparser.getAttributeCount();
for (int i = 0; i < nAttrNum; i++) {
sAttrName = xmlparser.getAttributeName(i);
sAttrValue = xmlparser.getAttributeValue(i);
if (sAttrName != null && sAttrName.equals("name")) {
sName = sAttrValue;
} else if (sAttrName != null && sAttrName.equals("age")) {
sAge = sAttrValue;
} else if (sAttrName != null && sAttrName.equals("height")) {
sHeight = sAttrValue;
}
}
if (sName != null && sAge != null && sHeight != null) {
sContent += "\t(" + sName + ", " + sAge + ", " + sHeight +")\n";
Log.d(TAG, sContent);
Log.d(TAG, "=====================");
}
xmlparser.next();
}
}
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
txtVContent.setText(sContent);
}
解法(二):
@SuppressLint("NewApi")
private void catResXmlFile2(int nXmlId) {
String sPeople = "";
String sName = "";
String sAge = "";
String sHeight = "";
String sContent = "";
String sAttrName = "";
String sAttrValue = "";
int nAttrNum = 0;
int nCnt = 0;
XmlPullParser xmlparser = this.getResources().getXml(nXmlId);
int nEvtType = 0;
try {
nEvtType = xmlparser.getEventType();
while (nEvtType != XmlPullParser.END_DOCUMENT) {
switch (nEvtType) {
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
sPeople = xmlparser.getName();
if (!sPeople.isEmpty() && sPeople.equals("person")) {
nAttrNum = xmlparser.getAttributeCount();
for (nCnt = 0; nCnt < nAttrNum; nCnt++) {
sAttrName = xmlparser.getAttributeName(nCnt);
sAttrValue = xmlparser.getAttributeValue(nCnt);
if (sAttrName != null && sAttrName.equals("name")) {
sName = sAttrValue;
} else if (sAttrName != null && sAttrName.equals("age")) {
sAge = sAttrValue;
} else if (sAttrName != null && sAttrName.equals("height")) {
sHeight = sAttrValue;
}
}
if (sName != null && sAge != null && sHeight != null) {
sContent += "\t(" + sName + ", " + sAge + ", " + sHeight +")\n";
Log.d(TAG, sContent);
Log.d(TAG, "=====================");
}
xmlparser.next();
}
break;
case XmlPullParser.END_TAG:
break;
case XmlPullParser.END_DOCUMENT:
break;
default:
break;
}
}
} catch (XmlPullParserException e) {
Log.d(TAG, "xmlparser error !");
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
txtVContent.setText(sContent);
}
}
以上只是解析最简单的xml格式。
(1)内部存储: 存放路径为/data/data/package_name/files/
(2)外部存储: SD卡
(3)扩充对资源xml文件的操作
一、内部存储
1.两个函数
openFileOutput 写入 权限:MODE_PRIVATE, MODE_APPEND, MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE
openFileInput 读取
2.写文件
FileOutputStream fos = openFileOutput(FILENAME, FILE_ACCESS);
String sContent = "12 34 56 789";
//getBytes Returns a new byte array containing the characters of this string encoded using the system's default charset.
fos.write(sContent.getBytes());
fos.flush(); //小量数据时系统存在内存中,可多次write后再调一次flush刷新到flash
fos.close();
3.读文件
FileInputStream fis = openFileInput(FILENAME);
byte[] buffer = new byte[fis.available()]; //该接口不保证等于文件的实际长度
while (fis.read(buffer) != -1){
}
String str = new String(buffer);
fis.close();
注意:以上读写文件操作都是not save的,需要进行try/catch
二、SD卡存储
和文件存储类似,操作sdcard文件前提是
(1)要确认sdcard已挂载。
(2)以绝对路径方式创建文件
(3)检测sdcard文件存在及可读写性
在已挂在的外部USB设备上进行读写,sdcard具有可读写权限.
eg:
public String SDCARD_PATH = "/mnt/vold/usbdisk1/sda1";
public void createFileInSdcard(String _filename) {
String sTestContent = "testx.txt";
FileOutputStream fos = null;
File dir = null;
File sdcardFile = null;
dir = new File(SDCARD_PATH);
if (dir.exists() && dir.canWrite()) {
sdcardFile = new File(dir.getAbsolutePath() + "/" + _filename);
try {
sdcardFile.createNewFile();
if (sdcardFile.exists() && sdcardFile.canWrite()) {
fos = new FileOutputStream(sdcardFile);
//fos =openFileOutput(sdcardFile, Context.MODE_WORLD_WRITEABLE + Context.MODE_WORLD_READABLE);
fos.write(sTestContent.getBytes());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
【注】上面File.createNewFile方法创建的文件默认属性为rwx-rwx-rwx, 如果不想ugo都具有rwx权限,在检测sdcard存在后创建文件的操作
可以参照文件内部存储的方式创建。
在AVD上模拟SDcard方法
(1)通过android-sdk的tools下的mksdcard工具映射成本地的文件(模拟sdcard)
mksdcard -l sdcard 128M E:\android_proj\AVDSDcard
-l sdcard 定义sdcard的标签
128M 定义sdcard的容量
E:\android_proj\AVDSDcard 是本地的路径(文件)
(2)在运行配置里添加属性
(3)要在AVD里指定sdcard文件的路径
eclipse -> windows->Android Virtual Device Manager
(4)启动AVD,往sdcard里上传一个文件,如果能成功,则sdcard创建成功
(5)以上通过adb 可以往sdcard里上传文件,但是通过程序往里面写的时候会包no permission,需要在AndroidMainifest.xml里加上写权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
三、资源文件
程序开发常用的资源文件分两种:
原始资源文件:如音视频文件等 --->/res/raw 目录下
XML文件: --->/res/xml 目录下
1.解析raw文件
(1)获取资源对象Resources
(2)打开资源文件
(3)读取资源inputstream流,然后按自己的要求执行
(4)关闭资源文件
private void catResRawFile(int nResId){
InputStream sInStream = null;
Resources res = this.getResources();
byte[] bReadBuf = null;
sInStream = res.openRawResource(nResId);
try {
bReadBuf = new byte[sInStream.available()];
while (sInStream.read(bReadBuf) != -1) {
}
txtVContent.setText(new String(bReadBuf/*, "utf-8"*/));
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e(TAG, e.getMessage(), e);
e.printStackTrace();
} finally {
if (sInStream != null) {
try {
sInStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e(TAG, e.getMessage(), e);
e.printStackTrace();
}
}
}
}
2.解析XML文件
例:
<people>
<person name="李某某" age="21" height="1.81" />
<person name="王某某" age="25" height="1.76" />
<person name="张某某" age="20" height="1.69" />
</people>
(1)获取资源对象Resources
(2)获取xml资源的xml解析器XmlPullParser (Android平台标准的解析器)
(3)逐个节点解析
解法(一):
public void catResXmlFile(int nXmlId){
String sPeople = "";
String sName = "";
String sAge = "";
String sHeight = "";
String sContent = "";
String sAttrName = "";
String sAttrValue = "";
int nAttrNum = 0;
XmlPullParser xmlparser = this.getResources().getXml(nXmlId);
try {
while (xmlparser.next() != XmlPullParser.END_DOCUMENT) {
sPeople = xmlparser.getName();
if (sPeople != null && sPeople.equals("person")) {
nAttrNum = xmlparser.getAttributeCount();
for (int i = 0; i < nAttrNum; i++) {
sAttrName = xmlparser.getAttributeName(i);
sAttrValue = xmlparser.getAttributeValue(i);
if (sAttrName != null && sAttrName.equals("name")) {
sName = sAttrValue;
} else if (sAttrName != null && sAttrName.equals("age")) {
sAge = sAttrValue;
} else if (sAttrName != null && sAttrName.equals("height")) {
sHeight = sAttrValue;
}
}
if (sName != null && sAge != null && sHeight != null) {
sContent += "\t(" + sName + ", " + sAge + ", " + sHeight +")\n";
Log.d(TAG, sContent);
Log.d(TAG, "=====================");
}
xmlparser.next();
}
}
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
txtVContent.setText(sContent);
}
解法(二):
@SuppressLint("NewApi")
private void catResXmlFile2(int nXmlId) {
String sPeople = "";
String sName = "";
String sAge = "";
String sHeight = "";
String sContent = "";
String sAttrName = "";
String sAttrValue = "";
int nAttrNum = 0;
int nCnt = 0;
XmlPullParser xmlparser = this.getResources().getXml(nXmlId);
int nEvtType = 0;
try {
nEvtType = xmlparser.getEventType();
while (nEvtType != XmlPullParser.END_DOCUMENT) {
switch (nEvtType) {
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
sPeople = xmlparser.getName();
if (!sPeople.isEmpty() && sPeople.equals("person")) {
nAttrNum = xmlparser.getAttributeCount();
for (nCnt = 0; nCnt < nAttrNum; nCnt++) {
sAttrName = xmlparser.getAttributeName(nCnt);
sAttrValue = xmlparser.getAttributeValue(nCnt);
if (sAttrName != null && sAttrName.equals("name")) {
sName = sAttrValue;
} else if (sAttrName != null && sAttrName.equals("age")) {
sAge = sAttrValue;
} else if (sAttrName != null && sAttrName.equals("height")) {
sHeight = sAttrValue;
}
}
if (sName != null && sAge != null && sHeight != null) {
sContent += "\t(" + sName + ", " + sAge + ", " + sHeight +")\n";
Log.d(TAG, sContent);
Log.d(TAG, "=====================");
}
xmlparser.next();
}
break;
case XmlPullParser.END_TAG:
break;
case XmlPullParser.END_DOCUMENT:
break;
default:
break;
}
}
} catch (XmlPullParserException e) {
Log.d(TAG, "xmlparser error !");
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
txtVContent.setText(sContent);
}
}
以上只是解析最简单的xml格式。