在Java中,可以通過以下方法來獲取相對(duì)路徑文件:
File file = new File("相對(duì)路徑");
Path path = Paths.get("相對(duì)路徑");
File file = path.toFile();
ClassLoader classLoader = getClass().getClassLoader();
URL url = classLoader.getResource("相對(duì)路徑");
File file = new File(url.getPath());
URL url = getClass().getResource("相對(duì)路徑");
File file = new File(url.getPath());
注意:在獲取相對(duì)路徑文件時(shí),需要確保該文件在classpath中存在,否則會(huì)出現(xiàn)文件找不到的異常。