java8 new Path
时间: 2023-07-04 12:06:37 浏览: 45
Java 8 introduces the `java.nio.file.Path` interface which represents a file system path and provides methods for working with file paths. Here is an example of creating a `Path` object:
```
Path path = Paths.get("C:\\myfolder\\myfile.txt");
```
This creates a `Path` object representing the file system path "C:\myfolder\myfile.txt". You can also use forward slashes instead of backslashes in the path:
```
Path path = Paths.get("C:/myfolder/myfile.txt");
```
The `Path` interface provides various methods for manipulating and accessing file paths. For example, you can use the `getParent()` method to get the parent directory of a file:
```
Path path = Paths.get("C:/myfolder/myfile.txt");
Path parent = path.getParent();
```
This returns a `Path` object representing the directory "C:\myfolder". You can also use the `getFileName()` method to get the file name:
```
Path path = Paths.get("C:/myfolder/myfile.txt");
Path fileName = path.getFileName();
```
This returns a `Path` object representing the file name "myfile.txt". There are many other methods available in the `Path` interface for working with file paths.
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)