android遊戲開發—04—操作SDcard

來源:互聯網
上載者:User

廢話不多說直接上代碼,十分簡單

package com.example.demogame;import java.io.File;import java.io.FileInputStream;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {Button but; // 開啟按鈕引用@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);but = (Button) findViewById(R.id.Button01);but.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {String contentResult = loadContentFromSDCard("AndroidSummary.txt");// 調用讀取檔案方法,獲得檔案內容EditText etContent = (EditText) findViewById(R.id.EditText01);// 執行個體化EditTextetContent.setText(contentResult);}});}public String loadContentFromSDCard(String fileName) {// 從SD卡讀取內容String content = null; // sd卡 的內容字串try {File f = new File("/sdcard/" + fileName);// 待讀取的檔案int length = (int) f.length();byte[] buff = new byte[length];FileInputStream fis = new FileInputStream(f);fis.read(buff); // 從此輸入資料流中將 byte.length 個位元組的資料讀入一個 byte 數組中fis.close(); // 關閉此輸入資料流並釋放與此流關聯的所有系統資源content = new String(buff, "UTF-8");} catch (Exception e) {Toast.makeText(this, "對不起,沒有找到檔案", Toast.LENGTH_SHORT).show();}return content;}}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.