android---(文件管理)

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

//以下文件均在 data/data/程序包名/
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    /**
     * 写入私有文件 会在当前应用程序包下创建一个 file 文件夹 并在此文件夹下创建一个zhang.txt文件
     *
     * @param view
     */
    public void writerPrivate(View view) {

        try {
            OutputStream out = openFileOutput("zhang.txt", Context.MODE_APPEND);

            String info = "中国人";
            byte[] bytes = info.getBytes();

            out.write(bytes, 0, bytes.length);
            out.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    /**
     * 读取私有文件内容 读取应用程序包下的 file文件夹下的指定文件 ,这里为 zhang.txt
     *
     * @param view
     */
    public void readSDK(View view) {
        try {
            InputStream in = openFileInput("zhang.txt");

            StringBuffer sb = new StringBuffer();

            byte[] bytes = new byte[100];
            int len = -1;

            while ((len = in.read(bytes)) != -1) {

                sb.append(new String(bytes, 0, len));
            }
            in.close();
            Toast.makeText(this, sb, Toast.LENGTH_SHORT).show();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /***
     * 读取 raw 文件,自已在res 文件夹下 创建的 raw文件夹下的文件 名称为固定写法
     *
     * @param view
     */
    public void readRawFile(View view) {

        InputStream in = getResources().openRawResource(R.raw.zhang);
        StringBuffer sb = new StringBuffer();

        byte[] bytes = new byte[100];
        int len = -1;

        try {
            while ((len = in.read(bytes)) != -1) {

                sb.append(new String(bytes, 0, len));
            }
            in.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        Toast.makeText(this, sb, Toast.LENGTH_SHORT).show();

    }

    /**
     * 创建缓存文件 在当前应用程序包下的 cache 文件夹下创建一个以 .tmp结尾的文件 并写入缓存内容
     *
     * @param view
     */
    public void readCacheFile(View view) {
        //创建缓存文件路径
        //String temp =  getCacheDir()+"/temp.tmp";


        try {
            //快速创建缓存文件
            File tem = File.createTempFile("temp", null, getCacheDir());

            //临时文件写入内容
            FileOutputStream out = new FileOutputStream(tem);
            PrintStream ps = new PrintStream(out);
            ps.print("我是第一噢!");
            ps.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

//以下文件均在 sdcard 卡上
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

 /***
     * 判断外部是否有sdCard 存储卡
     *
     * @param view
     */
    public void isExtSdk(View view) {

        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            Toast.makeText(this, "sdk", Toast.LENGTH_SHORT).show();

            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
                Toast.makeText(this, "sd只读", Toast.LENGTH_SHORT).show();

            } else {
                //获取外部存储sdk 的路径
                Log.i("msg", Environment.getExternalStorageDirectory().getPath());

                //访问 android内置的文件夹 也就是 storage 的文件夹下的文件或文件夹 ,这里访问下载文件夹的路径
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

            }

        } else {
            Toast.makeText(this, " no sdk", Toast.LENGTH_SHORT).show();
        }

    }


    /***
     * 写入外部存储的私有文件 4.4以后才能使用
     * <p/>
     * //在本应用程序在外部存储卡上的 storage/sdcard/android/data/应用程序包/file文件夹下写入文件,
     * //只有本应用程序才能访问这个 文件
     *
     * @param view
     */
    public void writerSDK(View view) {
        //本应用程序包下的 file 文件夹
        File file = getExternalFilesDir(null);
        if (file != null) {
            try {
                FileOutputStream out = new FileOutputStream(file + "/zhangzhang.txt");
                Log.i("msg", file.getPath());
                PrintStream ps = new PrintStream(out);
                ps.print("zhang zhang");
                ps.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

    }


    /***
     * 写入外部存储的私有缓存文件 4.4以后才能使用
     *
     *  路径 storage/sdcard/android/data/应用程序包/cache文件夹下写入缓存文件,
     *
     * @param view
     */
    public void readprivateSDKtmp(View view) {

        try {
            //创建缓存文件
            File tempFile = File.createTempFile("zhan", null, getExternalCacheDir());

            if (tempFile != null) {
                FileOutputStream out = new FileOutputStream(tempFile);
                PrintStream ps = new PrintStream(out);
                ps.print("good is good");

                ps.close();
                out.close();
            }


        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值