Internal Storage
Internal Storage:內部儲存空間。
Writing file
先呼叫openFileOutput(FileName)取得取得該檔案的FileOutputStream,再將要寫入的資料傳入fOut.write(),記得在寫入完成後呼叫close()將fOut(output stream)關閉。
FileOutputStream fOut = openFileOutput("file name here",MODE_WORLD_READABLE);
String str = "data";
fOut.write(str.getBytes());
fOut.close();
FileInputStream fin = openFileInput(file);
int c;
String temp="";
while( (c = fin.read()) != -1){
temp = temp + Character.toString((char)c);
}
fin.close();
getChannel():取得FileChannel。
String str = "data";
fOut.write(str.getBytes());
fOut.close();
Reading file
使用openFileInput(FileName)取得取得該檔案的FileInputStream,再使用read()讀取資料input,讀取完畢後同樣以close()關閉。fin.read()一次讀1Byte。
FileInputStream fin = openFileInput(file);
int c;
String temp="";
while( (c = fin.read()) != -1){
temp = temp + Character.toString((char)c);
}
fin.close();
FileOutputStream Class
FileOutputStream(File file, boolean append):建立新的FileOutputStream供寫入文件。
getChannel():取得FileChannel。
getFD():取得檔案描述符。
write(byte[] buffer, int byteOffset, int byteCount) : 緩衝區(記憶體暫時儲存地方)寫入到檔案中,從緩衝區的byteOffset開始寫,寫入長度為byteCount。
FileInputStream class
available():
getChannel():取得FileChannel。
getFD():取得檔案描述符。
read(byte[] buffer,int byteOffset, int byteCount):讀取檔案的數據並存到緩衝區(記憶體暫時儲存地方),從byteOffset開始存儲,存儲長度為byteCount。
參考文獻
Android - Internal Storage
Android內部存儲教程
[Android] 內部儲存體(Internal Storage)的檔案系統讀寫(File I/O)
[java] 讀檔 (FileInputStream)
[JAVA] 使用FileInputStream&FileOutputStream讀取寫入二進制檔案
android 讀取, 寫入及刪除檔案 (internal storage)
Android數據存儲:Internal Storage
[java] 讀檔寫檔(FileInputStream)
[Android] Stream方式寫入檔案
http://style77125tech.pixnet.net/blog/post/17636390-%5Bandroid%5D-stream%E6%96%B9%E5%BC%8F%E5%AF%AB%E5%85%A5%E6%AA%94%E6%A1%88
Java IO流之文件輸入輸出流 [FileOutputStream(File file, boolean append)]
Java.io.FileOutputStream類
FileInputStream 和 FileOutputStream 介绍(getFD)
檔案描述符(File descriptor)
Java NIO系列教程(7)︰FileChannel
沒有留言:
張貼留言