输入输出流_内部存储·

public void bt3(View v){

        try {
             //从内存写入文件
            //1.得到内部存储目录
                File file=getFilesDir();
        String s=file.getAbsolutePath();
        Toast.makeText(MainActivity.this, "path="+s, Toast.LENGTH_SHORT).show();

        //2.用输出流写入文件
        FileOutputStream fos=openFileOutput("test.txt", MODE_PRIVATE);

        //3.写入文件内容
        PrintStream ps=new PrintStream(fos);
        String ss=et.getText().toString();
        ps.println(ss);
        ps.print("测试");
        ps.close();
        fos.close();
        Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();}
        catch (Exception e){

        }
    }
    public  void bt4(View v){
        try {
        //输入流
        FileInputStream fis=openFileInput("test.txt");

            //定义byte[]
            byte []b=new byte[1024];
            int i;

            //循环读
            String str1="";
            while ((i=fis.read(b))>0){
                str1 =new String(b,0,i);
              }
            et2.setText(str1);
            fis.close();

    } catch (Exception e){

        }
    }

    public  void bt5(View v){
        try{
            //1.得到assetsManager

            AssetManager assetManager = getAssets();

            //2.操作资产目录,边读边写入
            //1) 读文件到内存 inputstream
            InputStream inputStream = assetManager.open("aa.jpg");

            //2) 写文件到目录 outputstream
            FileOutputStream fos=openFileOutput("a.jpg",MODE_PRIVATE);

            //先读后写
            byte [] b=new byte[1024];
            int i=0;
            while ((i=inputStream.read(b))>0)
            {
                fos.write(b,0,i);
            }
            fos.close();
            inputStream.close();


            //先写后读
            Toast.makeText(MainActivity.this, "保存文件成功", Toast.LENGTH_SHORT).show();
        }
        catch(Exception e){
            Toast.makeText(MainActivity.this, "保存文件出错", Toast.LENGTH_SHORT).show();
        }
    }

    //设置图片指向内部存储
    public  void bt6(View v){
        //得到文件路径
        String path=getFilesDir().getAbsolutePath()+"/a.jpg";

        //从内部存储的图片得到 Bitmap
        //BitmapFactory.decodeFile("文件路径");
        Bitmap bm= BitmapFactory.decodeFile(path);

        //设置图片视图的图片来源
        iv.setImageBitmap(bm);
    }
原文地址:https://www.cnblogs.com/storm47/p/5536828.html