判断一个文件是否是符号链接

来源:互联网 发布:梦入神机 知乎 编辑:程序博客网 时间:2024/05/16 01:40
    public static boolean isSymlink(File file) throws IOException {        if (file == null) {            throw new NullPointerException("File must not be null");        }                File canon;        if (file.getParent() == null) {            canon = file;        } else {            File canonDir = file.getParentFile().getCanonicalFile();            canon = new File(canonDir, file.getName());        }        return !canon.getCanonicalFile().equals(canon.getAbsoluteFile());    }

2. 获得文件总大小

public static long getTotalSizeOf(final String storagePath) {if (TextUtils.isEmpty(storagePath)) {return 0;}// 尝试多加判断,如果无效的参数 StatFs 会报错File file = new File(storagePath);boolean isSymLink = false;try {isSymLink = FileUtil.isSymlink(file);} catch (IOException e) {e.printStackTrace();}if (!file.exists() || !file.isDirectory() || isSymLink) {return 0;}StatFs stat = new StatFs(storagePath);long blockSize = stat.getBlockSize();long blockCount = stat.getBlockCount();return blockCount * blockSize;}

3. 可以获得的大小

public static long getAvailableSizeOf(final String storagePath) {if (TextUtils.isEmpty(storagePath)) {return 0;}// 尝试多加判断,如果无效的参数 StatFs 会报错File file = new File(storagePath);boolean isSymLink = false;try {isSymLink = FileUtil.isSymlink(file);} catch (IOException e) {e.printStackTrace();}if (!file.exists() || !file.isDirectory() || isSymLink) {return 0;}StatFs stat = new StatFs(storagePath);long blockSize = stat.getBlockSize();long availableBlocks = stat.getAvailableBlocks();return availableBlocks * blockSize;}

0 0
原创粉丝点击