Java的File类提供的接口仅限于判断目录和文件,有时候在复制某些数据的时候,还需要判断其它的文件类型,比如管道文件,需要过滤掉,不然IO操作的时候会阻塞。
这里我们用到的是Android提供的stat操作,读取文件的mode,第一个字节是文件类型,后3个字节是权限
/**
* file type mask
*/
public static final int S_IFMT = 0xf000;
/**
* file type is pipe
*/
public static final int S_FIFO = 0x1000;
public static boolean isPipe(String filePath) {
boolean result = false;
StructStat structStat = null;
try {
structStat = Os.stat(filePath);
int mode = structStat.st_mode;
switch (mode & S_IFMT) {
case S_FIFO:
result = true;
break;
default:
break;
}
} catch (ErrnoException e) {
e.printStackTrace();
}
return result;
}