Android資料儲存之File
(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格式。