您的位置:首页 > 其它

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

2014-01-10 16:08 393 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: