android資料存放區,

來源:互聯網
上載者:User

android資料存放區,

 

app在運行過程中,需要儲存使用者的一些資訊。例如登入狀態、賬戶資訊等,安卓提供了多種方式來儲存使用者的資料。

 

1.key-value 儲存,通過SharedPreferences實現 

SharedPreferences sp = getSharedPreferences("test", Context.MODE_PRIVATE); 

sp.edit().putString("str", "hello").commit();

Log.e(TAG, "save1: "+sp.getString("str",null) );  

2.檔案儲存,把資料寫入內部儲存空間中 

    File file = getFilesDir();

    Log.e(TAG, "save2: "+file.getAbsolutePath());

 

    File f2 = new File(file,"test.log");

    try {

      f2.createNewFile();

      //在檔案中寫入資料

      Log.e(TAG, "save2: "+f2.exists());

    } catch (IOException e) {

      e.printStackTrace();

    } 

3.資料庫儲存,通過SQLite 資料庫讀寫結構化資料

通過openOrCreateDatabase建立資料庫或者繼承SQLiteOpenHelper實現

 

4.網路儲存,通過http協議把資料儲存在伺服器上

 

  URL url = new URL("http://www.android.com/");
   HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
   try {
     InputStream in = new BufferedInputStream(urlConnection.getInputStream());
     readStream(in);
   } finally {
     urlConnection.disconnect();
   }

 執行個體下載:https://files.cnblogs.com/files/bruce2020/TestSQLite.zip 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.