4.12 android存储方式——File

File

权限

<!-- SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- SDCard写入数据权限 -->
<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(访问环境的变量)

//判断SD卡是否可用  true
 boolean b= Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);

//SD卡根目录的绝对路径  /storage/emulated/0
 File file= Environment.getExternalStorageDirectory();

//获取内部存储的绝对路径 /data
String path= Environment.getDataDirectory().getAbsolutePath();

File(操作文件)

 /*操作文件*/
 //获取File对象
 File myfile=new File(path);
 //是否是文件夹
 myfile.isDirectory();
 //是否是文件
 myfile.isFile();
 //文件是否存在
 myfile.exists();
 //创建新文件
 try {
     myfile.createNewFile();
 } catch (IOException e) {
     e.printStackTrace();
 }
 //删除文件
 myfile.delete();
 //如果当前file对象是文件夹,则可以获得文件夹内包含的所有文件
File[] ffs= myfile.listFiles();
 //文件的长度
 myfile.length();
 //文件重命名
 myfile.renameTo(new File(""));
 //创建空的文件夹
 myfile.mkdir();

FileOutputStream与FileInputStream 文件IO流

/*将内容写入文件*/
private void saveFile(Context context,String FilePath,String content){
    try {
       FileOutputStream fileOutputStream= openFileOutput(FilePath,MODE_PRIVATE);
        fileOutputStream.write(content.getBytes());
        fileOutputStream.flush();
        fileOutputStream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


}
/*SD卡中读取文件内容*/
private String readSDFile(String FileName){
    String res="";
    try {
    File file=new File(FileName);
    if(file.exists()==true){
        InputStreamReader inputStreamReader = null;
       
            inputStreamReader = new InputStreamReader(new FileInputStream(file), "UTF-8");
       
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String lineStr;
        while ((lineStr = bufferedReader.readLine()) != null) {
            res += lineStr;
        }
        inputStreamReader.close();

    }else{
        return  null;

    }


    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException 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);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值