Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android讀寫sd卡操作寫入數據讀取數據示例

android讀寫sd卡操作寫入數據讀取數據示例

編輯:關於Android編程

復制代碼 代碼如下:
package com.example.sdcardtest;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;

import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;

public class MainActivity extends Activity {
    final String FILE_NAME = "/lxp.bin";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button write = (Button) findViewById(R.id.main_btn1);
        Button read = (Button) findViewById(R.id.main_btn2);
        final EditText edit1 = (EditText) findViewById(R.id.main_edit1);
        final EditText edit2 = (EditText) findViewById(R.id.main_edit2);
        write.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                write(edit1.getText().toString().trim());
                edit1.setText("");
            }
        });
        read.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                edit2.setText(read());
            }
        });
    }
    private void write(String content){
        try {
            if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                //獲取SD卡的目錄
                File sdCardDir = Environment.getExternalStorageDirectory();
                File targetFile = new File(sdCardDir.getCanonicalPath() + FILE_NAME);
                //以指定文件創建RandomAccessFile對象
                RandomAccessFile raf = new RandomAccessFile(targetFile, "rw");
                //將文件記錄指針移動到最後
                raf.seek(targetFile.length());
                //輸出文件內容
                raf.write(content.getBytes());
                raf.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String read(){
        try {
            if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                //獲得SD卡對應的存儲目錄
                File sdCardDir = Environment.getExternalStorageDirectory();
                //獲取指定文件對應的輸入流
                FileInputStream fis = new FileInputStream(sdCardDir.getCanonicalPath() + FILE_NAME);
                //將指定輸入流包裝成BufferReader
                BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                StringBuilder sb = new StringBuilder("");
                String line = null;
                //循環讀取文件內容
                while((line = br.readLine()) != null){
                    sb.append(line);
                }
                br.close();
                return sb.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

最後別忘了在AndroidManafest.xml中增加訪問讀取SD的權限。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved