Android開發學習——android儲存,android開發儲存體
Android的儲存
內部儲存空間
RAM記憶體:運行記憶體,相當於電腦的記憶體
ROM記憶體:儲存記憶體,相當於電腦的硬碟
外部儲存空間
SD卡:相當於電腦的移動硬碟
* 2.2之前,sd卡路徑:sdcard
* 4.3之前,sd卡路徑:mnt/sdcard
* 4.3開始,sd卡路徑:storage/sdcard
* 所有存放裝置,都會被劃分成若干個區塊,每個區塊有固定的大小
* 存放裝置的總大小 = 區塊大小 * 區塊數量
檔案存取權限
* 指的是誰能訪問這個檔案
* 在Android中,每一個應用,都是一個獨立的使用者
* 使用10個字母表示 drwxrwxrwx
* 第一個字母:
* d:表示檔案夾
* -:表示檔案
* 第一組rwx:表示的是檔案擁有者(owner)對檔案的許可權
* r:read,讀
* w:write
* x:execute
* 第二組rwx:表示的是跟檔案擁有者屬於同一使用者組的使用者(grouper)對檔案的許可權
* 第三組rwx:表示的其他使用者(other)對檔案的許可權
在內部儲存空間中讀寫檔案
小案例:使用者輸入帳號密碼,勾選“記住帳號密碼”,點擊登入按鈕,登入的同時持久化儲存帳號和密碼
布局檔案:
<LinearLayout 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" tools:context=".MainActivity" android:orientation="vertical" > <EditText android:id="@+id/et1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="使用者名稱:" /> <EditText android:id="@+id/et2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="密 碼:" /> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="記住使用者名稱和密碼" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="login" android:text="登 錄" /></LinearLayout>
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void login(View v){ EditText et1 = (EditText) findViewById(R.id.et1); EditText et2 = (EditText) findViewById(R.id.et2); String username = et1.getText().toString(); String pwd = et2.getText().toString(); // 判斷使用者是否勾選儲存帳號密碼 CheckBox cb = (CheckBox) findViewById(R.id.checkBox1); if(cb.isChecked()){ //將使用者名稱和密碼寫到本地檔案,用IO流來寫 File file = new File("data/data/com.example.cunchu/info.txt");//內部儲存空間的路徑 FileOutputStream fos; try { fos = new FileOutputStream(file); fos.write((username+"####"+pwd).getBytes()); fos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.print("登入成功!!!"); Toast.makeText(this,"登入成功!!!", Toast.LENGTH_SHORT).show(); } }public void read(){ try { FileInputStream fis = new FileInputStream("data/data/com.example.cunchu/info.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String text = br.readLine(); String []s = text.split("####"); EditText et1 = (EditText) findViewById(R.id.et1); EditText et2 = (EditText) findViewById(R.id.et2);
//讀取到資料之後,回顯至輸入框 et1.setText(s[0]); et2.setText(s[1]); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
運行結果:
使用路徑api讀寫檔案
getFilesDir()得到的file對象的路徑是data/data/com.itheima.rwinrom2/files
* 存放在這個路徑下的檔案,只要你不刪,它就一直在
getCacheDir()得到的file對象的路徑是data/data/com.itheima.rwinrom2/cache
* 存放在這個路徑下的檔案,當記憶體不足時,有可能被刪除
系統管理應用介面的清除緩衝,會清除cache檔案夾下的東西,清除資料,會清除整個包名目錄下的東西
如果有時需要直接複製項目
需要改動的地方:
* 項目名字
* 應用程式套件名
* R檔案重新導包
在外部儲存空間中讀寫檔案
在內部儲存讀寫和外部儲存 讀寫 檔案,都是用IO流讀寫,不同的是,路徑不同。