Android文件存储之SDcard

2.SDcard存储:(唯一的外部存储)
(1)Sdcard属于外部存储,因此程序保存的数据可以被其他项目访问,保存数据的程序删除后,数据不会被删除;
(2)SDcard存储项目必须配置权限:SDcard创建和删除文件权限,写入权限;

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

(3)保存文件步骤:
1.得到sdcard的目录 “/mnt/sdcard”
String sdcard =Environment.getExternalStorageDirectory().toString();

2.创建保存数据的文件,文件的名字可以是任意的,比如可以没有后缀名
File file = new File(sdcard+”/school”); file.createNewFile();//创建文件

3.得到文件输出流
4.使用流输出数据,保存到文件中
5.关闭流

取出sdcard目录

public class MainActivity extends Activity {
    private String sdcard = null;// sdcard的目录

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.sdcard = Environment.getExternalStorageDirectory().toString();//取出sdcard的目录
    }

保存数据

public void save(View v) {
try {
    //(2)创建保存数据的文件,
    File file = new File(sdcard + "/school");
    if(!file.exists()) {//如果文件不存在,则创建文件
        file.createNewFile();//创建文件
    }
    //(3)得到文件输出流
//          OutputStream out = new FileOutputStream(file);//替换
//          OutputStream out = new FileOutputStream(file, false);//替换
    OutputStream out = new FileOutputStream(file, true);//追加

    //(4)使用流输出数据,保存到文件中
    String data = "你好";
    out.write(data.getBytes());//保存
//          (5)关闭流
    out.close();
} catch (Exception e) {
    e.printStackTrace();
}
}

取出数据

//  步骤2:取出数据
//  (1)得到sdcard的目录 
//  (2)创建文件输入流
//  (3)通过输入流取数据
//  (4)关闭流
public void fetch(View v) {
    try {
        //(2)创建文件输入流 
        File file = new File(sdcard + "/school");
        InputStream in = new FileInputStream(file);
        //(3)通过输入流取数据
        byte[] data = IOUtils.read1(in, in.available(), 4096); 
//          byte[] data = IOUtils.read2(new BufferedInputStream(in), in.available(), 4096); 
//IOUtils是文件操作的一个工具类
        String str = new String(data);
        Toast.makeText(this, str, Toast.LENGTH_LONG).show();
//          (4)关闭流
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

IOUtils工具类

package xena.util;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

public class IOUtils {
    /**
     * @param in
     * @param size--->表示一定要取多少个字节
     * @param max---建意写成4096
     * @return  如果没有取到size个字节的数据,该方法不会返回,要么继续取,要么阻塞
     */
    public static byte[] read1(InputStream in, int size, int max) {
//      byte[] b = new byte[20];
//      in.read(b, 0, 20);
//      //从in中最多读取7个字节,存在b中,从b[3]开始存储, 

        byte[] buffer = new byte[size];
        int hasRead = 0;
        while (true) {
            if (max > size - hasRead) {
                max = size - hasRead;
            }
            try {
                hasRead = hasRead + in.read(buffer, hasRead, max);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (hasRead == size) {
                break;
            }
        }
        return buffer;
    }

    /**
     * 
     * @param bin
     * @param size--->表示一定要取多少个字节
     * @param max
     * @return  没有取到size个字节的数据,该方法不会返回,要么继续取,要么阻塞
     */
    public static byte[]  read2(BufferedInputStream bin, int size, int max) {
        byte[] image = new byte[size];
        int hasRead = 0;
        while (true) {
            if (max > size - hasRead) {
                max = size - hasRead;
            }
            try {
                hasRead = hasRead + bin.read(image, hasRead, max);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (hasRead == size) {
                break;
            }
        }
        return image;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值