Android_储存_内部储存和外部SD卡储存_笔记

 

 一, 分类:

        1, 内部存储 -- Internal Storage -- 私有的        -- 不要权限
    
            特点:
                1,文件只能被当前应用程序访问
                2,属于应用程序自己, 程序卸载后, 文件也会被删除

            存储的路径:
                data/data/应用程序包名/files / ***.***

            使用方式:

                存:
                    //TODO 1 , 打开文件输出流  参数: String name  文件名称(带后缀), int mode  文件的操作模式
                    FileOutputStream fos = openFileOutput("set.txt",MODE_PRIVATE);
                    //TODO 2, 写入数据
                    String msg = "这是写入文件的数据";
                    fos.write(msg.getBytes());
                    //TODO 3, 关闭流
                    fos.close();

                取:

                     //TODO 1,  打开文件输入流
                    FileInputStream fis = openFileInput("set.txt");
                    //TODO 2, 读取数据
                    String msg = new String(ByteStreams.toByteArray(fis));
                    //TODO 3, 关闭流
                    fis.close();

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
//  TODO 写
    public void save(View view) {
        try {
            FileOutputStream fos = openFileOutput("set.txt", MODE_PRIVATE);
            String msg = "这是写入文件的数据";
            fos.write(msg.getBytes());
            fos.close();
            Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//  TODO 读
    public void read(View view) {
        try {
            FileInputStream fis = openFileInput("set.txt");
            String msg = new String(ByteStreams.toByteArray(fis));  //TODO 需要加 implementation 'com.google.guava:guava:24.0-android'	//guava依赖
            fis.close();
            Toast.makeText(this, msg , Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

        2, 外部存储  - SD卡 -- External Storage -- 私有/公共    -- 需要添加权限

              特点:
                    私有内容: 程序卸载后, 文件删除     -- 只有自己访问
                    公共内容:  程序卸载后, 文件依然存在 -- 大家都可以访问

              路径:

                    mnt/sdcard 

              sd卡必须添加权限
                
                读:<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
                写: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

              使用方式:

                存:

                    //TODO 1, 得到sd卡中,写入文件的路径
                    //TODO Environment.getExternalStorageDirectory()  得到sd卡的根目录
                    File file = new File(Environment.getExternalStorageDirectory(),"msg.txt");
                    //TODO 2, 声明FileOutputStream
                    FileOutputStream fis = new FileOutputStream(file);
                    //TODO 3,通过FileOutputStream 写出数据
                    String msg = "我是sd卡储存";
                    fis.write(msg.getBytes());
                    //TODO 4, 关流
                    fis.close();

                取:

                    //TODO 1, 得到sd卡中,需要读取的文件路径
                    File file = new File(Environment.getExternalStorageDirectory(),"msg.txt");
                    //TODO 2, 声明FileInputStream
                    FileInputStream fis = new FileInputStream(file);
                    //TODO 3, 流 -- String
                    String msg = new String(ByteStreams.toByteArray(fis));
                    //TODO 4, 关流
                    fis.close();
 

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
//  TODO 写
    public void save(View view) {
        try {
            File file = new File(Environment.getExternalStorageDirectory(), "msg.txt");
            FileOutputStream fos = new FileOutputStream(file);
            String msg = "我是sd卡储存" ;
            fos.write(msg.getBytes());
            fos.close();
            Toast.makeText(this, "sd卡储存成功", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//  TODO 读
    public void read(View view) {
        try {
            File file = new File(Environment.getExternalStorageDirectory(), "msg.txt");
            FileInputStream fis = new FileInputStream(file);
            String msg = new String(ByteStreams.toByteArray(fis));
            fis.close();
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值