在Android中,我們可以將一些數(shù)據(jù)直接以文件的形式保存在設備中。例如:一些文本文件、PDF文件、音視頻文件和圖片等。Android 提供了文件讀寫的方法。

通過 Context.openFileInput()方法獲得標準Java文件輸入流(FileInputStream),通過Context.openFileOutput()方法獲得標準Java文件輸出流( FileOutputStream )。使用
Resources.openRawResource(R.raw.myDataFile)方法返回InputStream。
示例如下,新建一個Activity,添加兩個TextView和兩個Button,點擊第一個Button,將TextView上的數(shù)據(jù)寫到文件中,點擊第二個Button,將文件中的數(shù)據(jù)寫到TextView中。
MainActivity.java:
public class MainActivity extends Activity {
private String filename = "file.txt";
private TextView mytext1,mytext2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mytext1=(TextView)this.findViewById(R.id.text1);
mytext2=(TextView)this.findViewById(R.id.text2);
Button button1=(Button)this.findViewById(R.id.btn_read);
Button button2=(Button)this.findViewById(R.id.btn_write);
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mytext2.setText(read());
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
write(mytext1.getText().toString());
}
});
}
protected String read(){
try {
FileInputStream fis = openFileInput(filename);
try {
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
return new String(buffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void write(String str) {
try {
FileOutputStream fos = openFileOutput(filename,MODE_APPEND);
try {
fos.write(str.getBytes());
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:id="@+id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="我是text1"/> <Button android:id="@+id/btn_write" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="write"/> <TextView android:id="@+id/text2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="我是text2"/> <Button android:id="@+id/btn_read" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="read"/> </LinearLayout>
運行結果如下: 點擊write將第一個TextView寫入文件,點擊read將數(shù)據(jù)讀出到第二個TextView

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網(wǎng)站名稱:Android數(shù)據(jù)存儲(二)文件的使用-創(chuàng)新互聯(lián)
URL標題:http://chinadenli.net/article36/djoopg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、標簽優(yōu)化、營銷型網(wǎng)站建設、Google、建站公司、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)