在windows中,有一种很常见的文件——快捷方式。点击一下就可以跳转到指定的位置,可以是网址,也可以是文件目录,也可以是某个文件。
其中,跳转到网页的是url快捷方式,默认的后缀名是".url",只是被系统隐藏了。可以调出来。
跳转到文件,文件夹的是lnk快捷方式。默认的后缀名是".lnk",也一样被系统隐藏了。可以调出来。
于是,可以通过后缀名的方式判断文件是否是一个快捷方式。
而我要说的是另一种方法。
先说lnk快捷方式,lnk文件在定义的时候,是有一定的标准的。其文件头固定值为一个数字,十六进制——4c
十进制——76。
所以,也可以通过io流来判断,只是,io流,或许消耗资源高,以下是方法:
/**
* @param file
* @return 是否是lnk文件格式
*/
public static boolean isInkFile(File file){
BufferedInputStream bis = null;
InputStream is = null;
int firstInt = 0;
try {
is = new FileInputStream(file);
bis = new BufferedInputStream(is);
firstInt = bis.read();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (firstInt == 0x4c) {//76,L,代表lnk文件格式
return true;
}
return false;
}
而通过后缀名来判断,需要忽略大小写,考虑使用正则表达式:]
/**
* @param filePath
* @return
*/
public static boolean isEndWithWithLnk(String filePath){
Pattern p = Pattern.compile("\\.(?i)lnk");//忽略大小写
Matcher m = p.matcher(filePath.substring(filePath.length()-4));
return m.find();
}
这样做有个缺点,检测到的快捷方式不一定是标准的快捷方式。
windows 可能就是通过后缀名判断的,当将一个txt文件的后缀改为lnk后,就直接有小箭头图标了。