編輯:關於Android編程
以下內容給大家介紹Android數據存儲提供了五種方式:
1、SharedPreferences
2、文件存儲
3、SQLite數據庫
4、ContentProvider
5、網絡存儲
本文主要介紹如何使用文件來存儲數據。Android文件操作用到的是Java.IO中的FileOutputStream和FileInputStream類。
一、存儲文件
首先實例化一個FileOutputStream。
FileOutputStream foStream = openFileOutput(fileName, MODE_PRIVATE);
// fileName: 要寫入文件的名稱
// MODE_PRIVATE: 為默認操作模式,代表該文件是私有數據,只能被應用本身訪問,在該模式下,寫入的內容會覆蓋原文件的內容
// MODE_APPEND: 模式會檢查文件是否存在,存在就往文件追加內容,否則就創建新文件.
// MODE_WORLD_READABLE: 表示當前文件可以被其他應用讀取,不推薦使用
// MODE_WORLD_WRITEABLE: 表示當前文件可以被其他應用寫入,不推薦使用
然後調用foStream.write()即可完成寫入。
byte[] buffer = fileContent.getBytes();
foStream.write(buffer);
Toast.makeText(MainActivity.this, "寫入成功",Toast.LENGTH_SHORT).show();
最後進行一些清理工作,刷新寫出流和關閉流。
foStream.flush();
foStream.close();
二、讀取文件
同樣的,首先實例化一個FileInputStream。
FileInputStream fiStream = openFileInput(fileName)
然後調用fiStream.read()即可
int len = fiStream.available();
byte[] buffer = new byte[len];
fiStream.read(buffer)
最後,將文本顯示並關閉讀文件流
etContent.setText(new String(buffer));
Toast.makeText(MainActivity.this, "讀取成功",Toast.LENGTH_SHORT).show();
fiStream.close();
三、完整代碼
import android.support.v.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
private EditText etName;
private EditText etContent;
private Button btnWrite;
private Button btnRead;
private String fileName = "";
private String fileContent = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = (EditText)findViewById(R.id.etName);
etContent = (EditText)findViewById(R.id.etContent);
btnWrite = (Button)findViewById(R.id.btnWrite);
btnRead = (Button)findViewById(R.id.btnRead);
btnWrite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fileName = etName.getText().toString();
fileContent = etContent.getText().toString();
try {
FileOutputStream foStream = openFileOutput(fileName, MODE_PRIVATE);
byte[] buffer = fileContent.getBytes();
foStream.write(buffer);
Toast.makeText(MainActivity.this, "寫入成功",Toast.LENGTH_SHORT).show();
foStream.flush();
foStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
});
btnRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fileName = etName.getText().toString();
try{
FileInputStream fiStream = openFileInput(fileName);
int len = fiStream.available();
byte[] buffer = new byte[len];
fiStream.read(buffer);
etContent.setText(new String(buffer));
Toast.makeText(MainActivity.this, "讀取成功",Toast.LENGTH_SHORT).show();
fiStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
});
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etName"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="文件名" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etContent"
android:layout_below="@+id/etName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="文件內容" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存"
android:id="@+id/btnWrite"
android:layout_alignTop="@+id/btnRead"
android:layout_toLeftOf="@+id/btnRead"
android:layout_toStartOf="@+id/btnRead" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="讀取"
android:id="@+id/btnRead"
android:layout_below="@+id/etContent"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
android:Spinner(下拉框)控件的使用
1.效果圖 2.創建頁面文件(main.xml) 3.創建下拉框的數據源 List list = new A
android 圖片放大縮小的邊界簡單的限制處理
android 圖片放大縮小的邊界簡單的限制處理首先,你要明白,即使是微信這樣出色的軟件對4邊界限制處理也不是很完善的。具體你可以在上邊界將圖片放大之後再縮小,等等。所以
Android RecyclerView滑動刪除和拖動排序
本篇是接著上面三篇之後的一個對RecyclerView的介紹,這裡多說兩句,如果你還在使用ListView的話,可以放棄掉ListView了。RecyclerView自動
Android自定義ViewPager實現個性化的圖片切換效果
第一次見到ViewPager這個控件,瞬間愛不釋手,做東西的主界面通通ViewPager,以及圖片切換也拋棄了ImageSwitch之類的,開始讓ViewPager來做。