更详细的内容:
Android 文件读写以及assets操作
----------------------------------------------------------------------------------------------------------------------------------------
参考:
http://www.cnblogs.com/wainiwann/p/3274386.html
#######################################################################
private void copyBigDataToSD(String strOutFileName)
{
try {
String path = PATH + "/" + strOutFileName;
InputStream myInput;
OutputStream myOutput = new FileOutputStream(path);
myInput = this.getAssets().open(strOutFileName);
byte[] buffer = new byte[1024];
int length = myInput.read(buffer);
while (length > 0) {
myOutput.write(buffer, 0, length);
length = myInput.read(buffer);
}
myOutput.flush();
myInput.close();
myOutput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//从assets 文件夹中获取文件并读取数据
public void getFromAssets(String fileName){
try {
File file = new File(PATH, fileName);
if (file.exists()) { //if the file is exist, return
file.delete();
}
file.createNewFile();
InputStream in = getResources().getAssets().open(fileName);
//获取文件的字节数
int lenght = in.available();
//创建byte数组
byte[] buffer = new byte[lenght];
//将文件中的数据读到byte数组中
in.read(buffer);
FileOutputStream out = new FileOutputStream(file);
out.write(buffer);
out.flush();
out.close();
Log.i(TAG, "------------------------------------load ok " + fileName);
//result = EncodingUtils.getString(buffer, ENCODING);
} catch (Exception e) {
e.printStackTrace();
Log.i(TAG, "--------------------------"+e.getMessage());
}
}
public void getFromAssets(String fileName){
try {
File file = new File(PATH, fileName);
if (file.exists()) { //if the file is exist, return
file.delete();
}
file.createNewFile();
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
InputStreamReader inputReader = new InputStreamReader( getResources().getAssets().open(fileName) );
BufferedReader bufReader = new BufferedReader(inputReader);
String line="";
String Result="";
while((line = bufReader.readLine()) != null) {
Result = line;
Result += "\n";
bw.write(Result);
bw.flush();
}
bw.close();
inputReader.close();
bufReader.close();
Log.i(TAG, "------------------------------------load ok "+fileName);
} catch (Exception e) {
e.printStackTrace();
Log.i(TAG, "--------------------------"+e.getMessage());
}
// return "can not access assets";
}