最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

實例詳解Android文件存儲數(shù)據(jù)方式

 更新時間:2016年01月12日 10:10:24   作者:Snail  
總體的來講,數(shù)據(jù)存儲方式有三種:一個是文件,一個是數(shù)據(jù)庫,另一個則是網(wǎng)絡(luò)。下面通過本文給大家介紹Android文件存儲數(shù)據(jù)方式,對android文件存儲數(shù)據(jù)相關(guān)知識感興趣的朋友一起學習吧

總體的來講,數(shù)據(jù)存儲方式有三種:一個是文件,一個是數(shù)據(jù)庫,另一個則是網(wǎng)絡(luò)。下面通過本文給大家介紹Android文件存儲數(shù)據(jù)方式。

1.文件存儲數(shù)據(jù)使用了Java中的IO操作來進行文件的保存和讀取,只不過Android在Context類中封裝好了輸入流和輸出流的獲取方法。

創(chuàng)建的存儲文件保存在/data/data/<package name>/files文件夾下。

2.操作。

保存文件內(nèi)容:通過Context.openFileOutput獲取輸出流,參數(shù)分別為文件名和存儲模式。
讀取文件內(nèi)容:通過Context.openFileInput獲取輸入流,參數(shù)為文件名。
刪除文件:Context.deleteFile刪除指定的文件,參數(shù)為將要刪除的文件的名稱。
獲取文件名列表:通過Context.fileList獲取files目錄下的所有文件名數(shù)組。

*獲取文件路徑的方法:

絕對路徑:/data/data/<package name>/files/filename

Context:Context.getFilesDir()可以獲取到"/data/data/<package name>/files"

3.四種文件保存的模式。

Context.MODE_PRIVATE 為默認操作模式,代表該文件是私有數(shù)據(jù),只能被應用本身訪問,在該模式下寫入的內(nèi)容會覆蓋原文件的內(nèi)容。

Context.MODE_APPEND 檢查文件是否存在,存在就往文件追加內(nèi)容,否則就創(chuàng)建新文件。
MODE_WORLD_READABLE 表示當前文件可以被其他應用讀取。
MODE_WORLD_WRITEABLE 表示當前文件可以被其他應用寫入。

在使用模式時,可以用"+"來選擇多種模式,比如openFileOutput(FILENAME, Context.MODE_PRIVATE + MODE_WORLD_READABLE);

下面通過程序來演示下文件存儲的使用。完整代碼下載:android_files.rar

/** 
* MainActivity 
* 
* @author zuolongsnail 
* 
*/ 
public class MainActivity extends Activity { 
private EditText writeET; 
private Button writeBtn; 
private TextView contentView; 
public static final String FILENAME = "setting.set"; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
writeET = (EditText) findViewById(R.id.write_et); 
writeBtn = (Button) findViewById(R.id.write_btn); 
contentView = (TextView) findViewById(R.id.contentview); 
writeBtn.setOnClickListener(new OperateOnClickListener()); 
} 
class OperateOnClickListener implements OnClickListener { 
@Override 
public void onClick(View v) { 
writeFiles(writeET.getText().toString()); 
contentView.setText(readFiles()); 
System.out.println(getFilesDir()); 
} 
} 
// 保存文件內(nèi)容 
private void writeFiles(String content) { 
try { 
// 打開文件獲取輸出流,文件不存在則自動創(chuàng)建 
FileOutputStream fos = openFileOutput(FILENAME, 
Context.MODE_PRIVATE); 
fos.write(content.getBytes()); 
fos.close(); 
} catch (Exception e) { 
e.printStackTrace(); 
} 
} 
// 讀取文件內(nèi)容 
private String readFiles() { 
String content = null; 
try { 
FileInputStream fis = openFileInput(FILENAME); 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
byte[] buffer = new byte[1024]; 
int len = 0; 
while ((len = fis.read(buffer)) != -1) { 
baos.write(buffer, 0, len); 
} 
content = baos.toString(); 
fis.close(); 
baos.close(); 
} catch (Exception e) { 
e.printStackTrace(); 
} 
return content; 
} 
} 

程序截圖:

提供一個文件存儲數(shù)據(jù)的工具類:

/** 
* 文件存儲數(shù)據(jù)方式工具類 
* 
* @author zuolongsnail 
*/ 
public class FilesUtil { 
/** 
* 保存文件內(nèi)容 
* 
* @param c 
* @param fileName 
* 文件名稱 
* @param content 
* 內(nèi)容 
*/ 
private void writeFiles(Context c, String fileName, String content, int mode) 
throws Exception { 
// 打開文件獲取輸出流,文件不存在則自動創(chuàng)建 
FileOutputStream fos = c.openFileOutput(fileName, mode); 
fos.write(content.getBytes()); 
fos.close(); 
} 
/** 
* 讀取文件內(nèi)容 
* 
* @param c 
* @param fileName 
* 文件名稱 
* @return 返回文件內(nèi)容 
*/ 
private String readFiles(Context c, String fileName) throws Exception { 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
FileInputStream fis = c.openFileInput(fileName); 
byte[] buffer = new byte[1024]; 
int len = 0; 
while ((len = fis.read(buffer)) != -1) { 
baos.write(buffer, 0, len); 
} 
String content = baos.toString(); 
fis.close(); 
baos.close(); 
return content; 
} 
}

以上通過實例詳解Android文件存儲數(shù)據(jù)方式,希望對大家今后的工作學習有所幫助。

相關(guān)文章

最新評論

闸北区| 安宁市| 嘉黎县| 崇仁县| 彭阳县| 策勒县| 克东县| 嘉禾县| 晋宁县| 会宁县| 大安市| 民县| 奈曼旗| 山丹县| 永兴县| 抚顺市| 桦南县| 区。| 信丰县| 景德镇市| 喜德县| 龙门县| 光泽县| 抚宁县| 镇雄县| 安泽县| 方正县| 云梦县| 碌曲县| 福建省| 大余县| 邢台市| 浦江县| 西充县| 武宣县| 如东县| 郸城县| 易门县| 衡山县| 清水河县| 涟源市|