android读写sd卡操作写入数据读取数据示例

Malak ·
更新时间:2024-11-14
· 889 次阅读

代码如下:
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的权限。

您可能感兴趣的文章:Android编程之判断SD卡状态的方法Android获取监听SD卡状态Android获取SD卡路径及SDCard内存的方法Android调用相机并将照片存储到sd卡上实现方法android将Bitmap对象保存到SD卡中的方法Android实现从网络获取图片显示并保存到SD卡的方法Android获取SD卡上图片和视频缩略图的小例子Android 获取内外SD卡路径几种方法总结Android判断SD卡是否已经挂载的方法Android编程实现读取本地SD卡图片的方法Android开发中使用外部应用获取SD卡状态的方法



示例 数据 sd sd卡 Android

需要 登录 后方可回复, 如果你还没有账号请 注册新账号