@author SeanDragon Create By 2017-09-01 11:16
| 32 | * Create By 2017-09-01 11:16 |
| 33 | */ |
| 34 | public final class ToolPath { |
| 35 | |
| 36 | /** |
| 37 | * 文件遍历的访问选项 |
| 38 | */ |
| 39 | private static final EnumSet<FileVisitOption> OPTS = EnumSet.of(FileVisitOption.FOLLOW_LINKS); |
| 40 | |
| 41 | /** |
| 42 | * 读取选项 |
| 43 | */ |
| 44 | private static final OpenOption[] READ = new StandardOpenOption[]{StandardOpenOption.READ}; |
| 45 | /** |
| 46 | * 写入选项 |
| 47 | */ |
| 48 | private static final OpenOption[] WRITE_CREATE = new StandardOpenOption[]{StandardOpenOption.WRITE, StandardOpenOption.CREATE}; |
| 49 | private static final OpenOption[] WRITE_CREATE_APPEND = new StandardOpenOption[]{StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.APPEND}; |
| 50 | |
| 51 | public static Path getPath(String first, String... more) { |
| 52 | if (ToolStr.isBlank(first)) { |
| 53 | throw new NullPointerException("first is null"); |
| 54 | } |
| 55 | return Paths.get(first, more).normalize(); |
| 56 | } |
| 57 | |
| 58 | public static Path getPath(URI uri) { |
| 59 | if (uri == null) { |
| 60 | throw new NullPointerException("uri is null"); |
| 61 | } |
| 62 | return Paths.get(uri).normalize(); |
| 63 | } |
| 64 | |
| 65 | public static boolean isExists(Path path) { |
| 66 | return Files.exists(path); |
| 67 | } |
| 68 | |
| 69 | public static boolean isFile(Path path) { |
| 70 | return Files.isRegularFile(path); |
| 71 | } |
| 72 | |
| 73 | public static boolean isDir(Path path) { |
| 74 | return Files.isDirectory(path); |
| 75 | } |
| 76 | |
| 77 | public static boolean canRead(Path path) { |
| 78 | return Files.isReadable(path); |
| 79 | } |
| 80 | |
| 81 | public static boolean canWrite(Path path) { |
| 82 | return Files.isWritable(path); |
| 83 | } |
| 84 | |
| 85 | public static String getContentType(Path path) throws IOException { |
| 86 | String type; |
| 87 | URL u = path.toUri().toURL(); |
| 88 | URLConnection uc = u.openConnection(); |
| 89 | type = uc.getContentType(); |
| 90 | return type; |
| 91 | } |