[url=http://jb51.net/article/37225.htm]<Android开发笔记之: 数据存储方式详解>[/url][/b]。Java牛人可能想要用Java本身的能力:
File File.createTempFile(String prefix, String suffix);
File File.createTempFile(String prefix, String suffix, File path);
这也是可以的,但要考虑Android系统的特性,也就是说所写的路径是否有权限。比如对于第一个方法,用的是"java.io.tmpdir"这个在Android当中就是"/sdcard",所以当没有SD卡时这个方法必抛异常。
[b]2. 所有资源文件都是只读的,运行时无法更改
[/b]因为,程序运行时是把Apk动态解析加载到内存中,也就是说,Apk是不会有变化的,它是无法被改变的。
[b]3. 所有的资源文件夹/res和/assets也都是只读的[/b],不可写入
如上面所说,Apk是在编译后是无法再改变的了。
实例
下面是一个实例,可以递归式的遍历/assets下面所有的文件夹和文件
package com.android.explorer;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.MimeTypeMap;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
/*
* Explore all stuff in /assets and perform actions specified by users.
*/
public class FileAntActivity extends ListActivity {
private static final String TAG = "FileAntActivity";
private AssetManager mAssetManager;
private static final String EXTRA_CURRENT_DIRECTORY = "current_directory";
private static final String EXTRA_PARENT = "parent_directory";
public static final String FILEANT_VIEW = "com.android.fileant.VIEW";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String current = null;
String parent = null;
if (intent != null && intent.hasExtra(EXTRA_CURRENT_DIRECTORY)) {
current = intent.getStringExtra(EXTRA_CURRENT_DIRECTORY);
}
if (current == null) {
current = "";
}
if (intent != null && intent.hasExtra(EXTRA_PARENT)) {
parent = intent.getStringExtra(EXTRA_PARENT);
}
if (parent == null) {
parent = "";
}
mAssetManager = getAssets();
if (TextUtils.isEmpty(parent)) {
setTitle("/assets");
} else {
setTitle(parent);
}
try {
// List all the stuff in /assets
if (!TextUtils.isEmpty(parent)) {
current = parent + File.separator + current;
}
Log.e(TAG, "current: '" + current + "'");
String[] stuff = mAssetManager.list(current);
setListAdapter(new FileAntAdapter(this, stuff, current));
} catch (IOException e) {
e.printStackTrace();
}
}
private class FileAntAdapter extends BaseAdapter {
private Context mContext;
private String[] mEntries;
private String mParentDirectory;
public FileAntAdapter(Context context, String[] data, String parent) {
mContext = context;
this.mEntries = data;
mParentDirectory = parent;
}
public int getCount() {
return mEntries.length;
}
public Object getItem(int position) {
return mEntries[position];
}
public long getItemId(int position) {
return (long) position;
}
public View getView(final int position, View item, ViewGroup parent) {
LayoutInflater factory = LayoutInflater.from(mContext);
if (item == null) {
item = factory.inflate(R.layout.fileant_list_item, null);
TextView filename = (TextView) item.findViewById(R.id.filename);
TextView fileinfo = (TextView) item.findViewById(R.id.fileinfo);
ImageButton action = (ImageButton) item.findViewById(R.id.action);
final String entry = mEntries[position];
filename.setText(entry);
boolean isDir = isDirectory(entry);
if (isDir) {
fileinfo.setText("Click to view folder");
action.setVisibility(View.GONE);
item.setClickable(true);
item.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FILEANT_VIEW);
intent.putExtra(EXTRA_CURRENT_DIRECTORY, entry);
intent.putExtra(EXTRA_PARENT, mParentDirectory);
startActivity(intent);
}
});
} else {
final String type =
MimeTypeMap.getSingleton().getMimeTypeFromExtension(getExtension(entry));
fileinfo.setText(type);
item.setClickable(false);
action.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String filepath = entry;
if (!TextUtils.isEmpty(mParentDirectory)) {
filepath = mParentDirectory + File.separator + filepath;
}
BufferedInputStream in = new BufferedInputStream(mManager.open(filepath));
// Do whatever you like with this input stream
}
});
}
}
return item;
}
}
/**
* Test Whether an entry is a file or directory based on the rule:
* File: has extension *.*, or starts with ".", which is a hidden files in Unix/Linux,
* otherwise, it is a directory
* @param filename
* @return
*/
private boolean isDirectory(String filename) {
return !(filename.startsWith(".") || (filename.lastIndexOf(".") != -1));
}
private String getExtension(String filename) {
int index = filename.lastIndexOf(".");
if (index == -1) {
return "";
}
return filename.substring(index + 1, filename.length()).toLowerCase();
}
}