android 五种存储方式——File

来源:互联网 发布:vscode如何编译less 编辑:程序博客网 时间:2024/06/11 09:36

android的五种存储方式分别是:
1,SharedPreferences
2,file
3,sqlite
4,contentproviter
5,http

File

权限

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

File文件的模式
Activity.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容。

Activity.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。

Activity.MODE_WORLD_READABLE:表示当前文件可以被其他应用读取;

Activity.MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。

Environment(访问环境的变量)

Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);//判断sd卡是否可用Environment.getExternalStorageDirectory();//sd卡根目录的绝对路径Environment.getDataDirectory().getAbsolutePath();//获取内部存储的绝对路径

StatFs(文件系统空间检索)

StatFs stat = new StatFs(filePath);//获取一个statfs对象stat.getBlockSizeLong();//获取单个数据块的长度(byte),api 18以前使用stat.getBlockSize();stat.getBlockCountLong();//获取数据块的数量,api 18以前使用stat.getBlockCount();stat.getAvailableBlocksLong();//获取可用数据块的数量,api 18以前使用stat.getAvailableBlocks();Formatter.formatFileSize(context, 1*1024);//将当前值格式化成文件大小形式的字符串

File(操作文件)

File file = new File(path);//获得file的对象file.isDirectory();//是否是文件夹file.isFile();是否是文件file.exists();//文件是否存在file.createNewFile();//创建一个新文件file.delete();//删除一个文件file.listFiles();//如果当前file对象是文件夹,则可以获得文件夹内包含的所有文件,File[]file.length();//文件长度,bytefile.renameTo();//文件重命名file.mkdir();//创建一个空的文件夹

FileOutputStream与FileInputStream 文件IO流

    /**     * 将内容写入文件     *     * @author     */    private void saveFile(Context context, String filePath, String content) {        try {            FileOutputStream outputStream = context.openFileOutput(filePath, Activity.MODE_PRIVATE);            outputStream.write(content.getBytes());            outputStream.flush();            outputStream.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }
    /**     * 读取文件内容     *     * @param filePath     * @return     */    public String readSDFile(String filePath) {        String res = "";        try {            File file = new File(filePath);            if (!file.exists()) return null;            InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file), "UTF-8");            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);            String lineStr;            while ((lineStr = bufferedReader.readLine()) != null) {                res += lineStr;            }            inputStreamReader.close();        } catch (Exception e) {            e.printStackTrace();        }        return res;    }

操作File的常用方法

    /**     * 查看文件大小,递归     *     * @param file     * @return     */    public static long getFileSize(File file) {        long size = 0;        if (file.isDirectory()) {            File files[] = file.listFiles();            for (int i = 0; i < files.length; i++) {                size = size + getFileSize(files[i]);            }        } else {            size = size + file.length();        }        return size;    }
    /**     * 删除某个文件夹,递归     *     * @param filePath     */    public static void deleteFile(String filePath) {        File file = new File(filePath);        if (file.exists() && file.isFile()) {//如果文件存在,并且是文件夹            file.delete();        } else if (file.exists() && file.isDirectory()) {//如果文件是文件夹            File[] childFiles = file.listFiles();            if (childFiles == null || childFiles.length == 0)                return;            for (int i = 0; i < childFiles.length; i++) {                deleteFile(childFiles[i].getPath());            }        }    }
    /**     * 文件重命名     *     * @param path    文件目录     * @param oldname 原来的文件名     * @param newname 新文件名     */    public static void renameFile(String path, String oldname, String newname) {        if (!oldname.equals(newname)) {//新的文件名和以前文件名不同时,才有必要进行重命名            File oldfile = new File(path + "/" + oldname);            if (!oldfile.exists()) {                return;//文件不存在            }            File newfile = new File(path + "/" + newname);            if (newfile.exists()) {//若在该目录下已经有一个文件和新文件名相同,则重命名成(2)                //已经存在!                newfile = new File(path + "/" + newname + "(2)");                oldfile.renameTo(newfile);            } else {                oldfile.renameTo(newfile);            }        }    }
    /**     * 获得SD卡总大小     */    public static String getSDAllSize(Context context) {        File path = getSDFile();        StatFs stat = new StatFs(path.getPath());        long blockSize = 0;        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {            blockSize = stat.getBlockSizeLong();        } else {            blockSize = stat.getBlockSize();        }        long totalBlocks = 0;        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {            totalBlocks = stat.getBlockCountLong();        } else {            totalBlocks = stat.getBlockCount();        }        return Formatter.formatFileSize(context, blockSize * totalBlocks);    }
    /**     * 获得sd卡剩余容量,即可用大小     */    public String getSDRemainingSize(Context con) {        File path = getSDFile();        StatFs stat = new StatFs(path.getPath());        long blockSize = stat.getBlockSize();        long availableBlocks = stat.getAvailableBlocks();        return Formatter.formatFileSize(con, blockSize * availableBlocks);    }
原创粉丝点击