Android 获取目录下所有文件信息(名字、大小)

private void getFiles(){
        File file = new File(GlobalConstant.POSLOGPATH);
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            File childFile = files[i];
            String childName = childFile.getName();
            String fileSizeString = formetFileSize(childFile);
            Log.e("iiii", "getFiles: "+childName);
            Log.e("iiii", "fileLength="+fileSizeString);
        }
    }

    private String formetFileSize(File file){
        String fileSize = "0B";
        if (file == null){
            fileSize = "文件不存在";
            return fileSize;
        }
        long fileLength = file.length();
        DecimalFormat df = new DecimalFormat("#.00");
        if (fileLength < 1024){
            fileSize = df.format((double) fileLength) + "B";
        }else if (fileLength < 1048576){
            fileSize = df.format((double) fileLength / 1024) + "KB";
        }else if (fileLength < 1073741824){
            fileSize = df.format((double) fileLength / 1048576) + "MB";
        }else{
            fileSize = df.format((double) fileLength / 1073741824) + "GB";
        }
        return fileSize;
    }

Android 获取目录下所有文件信息(名字、大小)_第1张图片

你可能感兴趣的:(android开发)