Paths类:创建路径Path
(1)创建本地路径:
static Path | get(String first, String... more) |
(2)创建网络URL路径:
static Path | get(URI uri) |
Path类:
(1)为某一个Path注册事件管理器WatchService,然后获得事件集合WatchKey,之后就可以获取具体事件的信息了。
WatchKey | register(WatchService watcher, WatchEvent.Kind<?>... events) Registers the file located by this path with a watch service. |
例子:
WatchService watchService = FileSystems.getDefault().newWatchService();
Paths.get("").register(watchService,StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,StandardWatchEventKinds.ENTRY_MODIFY,
StandardWatchEventKinds.OVERFLOW);
while (true){
WatchKey key = watchService.take();
for (WatchEvent event : key.pollEvents()){
WatchEvent.Kind kind= event.kind();
event.context();
}
}
(2)比较path路径的信息
boolean |
boolean | startsWith(Path other) |
int | compareTo(Path other) |
(3)绝对路径、父路径等参数信息
Files类:
(1)遍历文件
static Path | walkFileTree(Path start, FileVisitor<? super Path> visitor) Walks a file tree. |
FileVistor的直接子接口SimpleFileVisitor,SimpleFileVisitor提供了默认是实现,也就是说abstract方法可以override部分。
Path path1 = Files.walkFileTree(Paths.get("."), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println("正在访问:" + file);
if (file.startsWith("Sorting")) {
System.out.println("找到了");
return FileVisitResult.TERMINATE;
} else
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
System.out.println("正在访问文件:" + dir);
return FileVisitResult.CONTINUE;
}
});
(2)找到符合规则的path
static Stream<Path> | find(Path start, int maxDepth,BiPredicate<Path,BasicFileAttributes> matcher,FileVisitOption... options) |
Interface BasicFileAttributes
All Known Subinterfaces:DosFileAttributes, PosixFileAttributes
static <V extends FileAttributeView> | getFileAttributeView(Path path, Class<V> type,LinkOption... options) |
(3)文件属性视图
--AttributeView
--FileAttributeView
--AclFileAttributeView
--BasicFileAttributeView
--FileOwnerFileAttributeView
--PosixFileAttributeView
例子:
BasicFileAttributeView b = Files.getFileAttributeView(path, BasicFileAttributeView.class);
BasicFileAttributes attrs = b.readAttributes();
(4)直接将路径映射成输入流或输出流
static InputStream | newInputStream(Path path,OpenOption... options) Opens a file, returning an input stream to read from the file. |
static OutputStream | newOutputStream(Path path,OpenOption... options) Opens or creates a file, returning an output stream that may be used to write bytes to the file. |
static BufferedReader | newBufferedReader(Path path) Opens a file for reading, returning a |
static BufferedReader | newBufferedReader(Path path, Charset cs) Opens a file for reading, returning a |
static BufferedWriter | newBufferedWriter(Path path, Charset cs,OpenOption... options) Opens or creates a file for writing, returning a |
static BufferedWriter | newBufferedWriter(Path path, OpenOption... options) Opens or creates a file for writing, returning a |
static SeekableByteChannel | newByteChannel(Path path, OpenOption... options) Opens or creates a file, returning a seekable byte channel to access the file. |
static SeekableByteChannel | newByteChannel(Path path, Set<? extends OpenOption> options,FileAttribute<?>... attrs) Opens or creates a file, returning a seekable byte channel to access the file. |
参数:--OpenOption
--StandardOpenOption(对文件读写访问的控制)
APPEND If the file is opened for |
CREATE Create a new file if it does not exist. |
CREATE_NEW Create a new file, failing if the file already exists. |
DELETE_ON_CLOSE Delete on close. |
DSYNC Requires that every update to the file's content be written synchronously to the underlying storage device. |
READ Open for read access. |
SPARSE Sparse file. |
SYNC Requires that every update to the file's content or metadata be written synchronously to the underlying storage device. |
TRUNCATE_EXISTING If the file already exists and it is opened for |
WRITE Open for write access. |