在Java中,相对路径是相对于当前工作目录或类路径的路径。以下是几种常见的相对路径写法:
相对于当前工作目录
`./file.txt`:表示当前目录下的`file.txt`文件。
`folder/file.txt`:表示当前目录下的`folder`子目录中的`file.txt`文件。
相对于父目录
`../file.txt`:表示当前目录的父目录中的`file.txt`文件。
相对于类路径
`resources/file.txt`:表示类路径下的`resources`目录中的`file.txt`文件。
使用`System.getProperty`获取当前工作目录
```java
String currentRelativePath = System.getProperty("user.dir");
Path path = Paths.get(currentRelativePath);
String fileName = "file.txt";
Path filePath = path.resolve(fileName);
使用`Paths.get`获取相对路径
```java
Path currentRelativePath = Paths.get("");
String fileName = "file.txt";
Path filePath = currentRelativePath.resolve(fileName);
使用`Path.toAbsolutePath`获取绝对路径
```java
Path currentRelativePath = Paths.get("");
Path absolutePath = currentRelativePath.toAbsolutePath();
String fileName = "file.txt";
Path filePath = absolutePath.resolve(fileName);
请根据你的需求选择合适的写法来指定相对路径。