Files-Paths

本文介绍了Java NIO中Paths类用于创建Path的方法,包括本地路径和网络URL路径。Path类提供了事件管理器的注册以及获取路径的绝对路径和父路径等功能。Files类则用于遍历文件,支持使用FileVisitor接口,如SimpleFileVisitor。文章详细讲解了BasicFileAttributes接口及其子接口,并讨论了文件属性视图如AttributeView、FileAttributeView。此外,还提及了文件打开选项OpenOption,特别是StandardOpenOption,用于控制文件的读写访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Paths类:创建路径Path

(1)创建本地路径:

static Pathget(String first, String... more)

(2)创建网络URL路径:

 

static Pathget(URI uri)

Path类:

(1)为某一个Path注册事件管理器WatchService,然后获得事件集合WatchKey,之后就可以获取具体事件的信息了。

 

WatchKeyregister(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

endsWith(Path other)

booleanstartsWith(Path other)
intcompareTo(Path other)

(3)绝对路径、父路径等参数信息

 

Files类:

(1)遍历文件

 

static PathwalkFileTree(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>
V
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 InputStreamnewInputStream(Path path,OpenOption... options)

Opens a file, returning an input stream to read from the file.

static OutputStreamnewOutputStream(Path path,OpenOption... options)

Opens or creates a file, returning an output stream that may be used to write bytes to the file.

 

static BufferedReadernewBufferedReader(Path path)

Opens a file for reading, returning a BufferedReader to read text from the file in an efficient manner.

static BufferedReadernewBufferedReader(Path path, Charset cs)

Opens a file for reading, returning a BufferedReader that may be used to read text from the file in an efficient manner.

static BufferedWriternewBufferedWriter(Path path, Charset cs,OpenOption... options)

Opens or creates a file for writing, returning a BufferedWriter that may be used to write text to the file in an efficient manner.

static BufferedWriternewBufferedWriter(Path path, OpenOption... options)

Opens or creates a file for writing, returning a BufferedWriter to write text to the file in an efficient manner.

static SeekableByteChannelnewByteChannel(Path path, OpenOption... options)

Opens or creates a file, returning a seekable byte channel to access the file.

static SeekableByteChannelnewByteChannel(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 WRITE access then bytes will be written to the end of the file rather than the beginning.

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 access, then its length is truncated to 0.

WRITE

Open for write access.

    

 

 

 


 

 

    


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值