如何在java中定义相对路径(Windows)

这是我的项目的结构:

这是我项目的结构

我需要读取MyClass.java config.properties 。 我尝试使用相对路径执行此操作,如下所示:

 // Code called from MyClass.java File f1 = new File("..\\..\\..\\config.properties"); String path = f1.getPath(); prop.load(new FileInputStream(path)); 

这给了我以下错误:

 ..\..\..\config.properties (The system cannot find the file specified) 

如何在java中定义相对路径? 我正在使用jdk 1.6并在Windows上工作。

尝试这样的事情

 String filePath = new File("").getAbsolutePath(); filePath.concat("path to the property file"); 

因此,您的新文件指向创建它的路径,通常是项目主文件夹。

[编辑]

正如@cmc所说,

  String basePath = new File("").getAbsolutePath(); System.out.println(basePath); String path = new File("src/main/resources/conf.properties") .getAbsolutePath(); System.out.println(path); 

两者都给出相同的价值。

  File f1 = new File("..\\..\\..\\config.properties"); 

尝试访问文件的这条路径在Project目录中,然后就像这样访问文件。

 File f=new File("filename.txt"); 

如果您的文件位于OtherSources/Resources

 this.getClass().getClassLoader().getResource("relative path");//-> relative path from resources folder 

值得一提的是,在某些情况下

 File myFolder = new File("directory"); 

不指向根元素。 例如,当您将应用程序放在C:驱动器( C:\myApp.jar )上时, myFolder指向(窗口)

 C:\Users\USERNAME\directory 

代替

 C:\Directory 

首先,在这里看到绝对路径和相对路径之间的差异:

绝对路径始终包含查找文件所需的根元素和完整目录列表。

或者,需要将相对路径与另一路径组合以访问文件。

在构造函数File(String pathname)中,Javadoc的File类说明了这一点

路径名,无论是抽象的还是字符串forms,可以是绝对的或相对的。

如果要获取相对路径,则必须将当前工作目录的路径设置为fle或directory。尝试使用系统属性来获取此信息。作为您绘制的图片:

 String localDir = System.getProperty("user.dir"); File file = new File(localDir + "\\config.properties"); 

此外,您应该尽量避免使用类似的“。”,“../”,“/”和其他类似的文件位置相对路径,因为当文件移动时,它更难处理。

  public static void main(String[] args) { Properties prop = new Properties(); InputStream input = null; try { File f=new File("config.properties"); input = new FileInputStream(f.getPath()); prop.load(input); System.out.println(prop.getProperty("name")); } catch (IOException ex) { ex.printStackTrace(); } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } } } 

你也可以使用
Path path1 = FileSystems.getDefault()。getPath(System.getProperty(“user.home”),“downloads”,“somefile.txt”);

我在使用我的图像文件的相对路径将屏幕截图附加到ExtentReports时遇到问题。 执行时我的当前目录是“C:\ Eclipse 64-bit \ eclipse \ workspace \ SeleniumPractic”。 在此下,我为report.html和image.png屏幕截图创建了文件夹ExtentReports,如下所示。

 private String className = getClass().getName(); private String outputFolder = "ExtentReports\\"; private String outputFile = className + ".html"; ExtentReports report; ExtentTest test; @BeforeMethod // initialise report variables report = new ExtentReports(outputFolder + outputFile); test = report.startTest(className); // more setup code @Test // test method code with log statements @AfterMethod // takeScreenShot returns the relative path and filename for the image String imgFilename = GenericMethods.takeScreenShot(driver,outputFolder); String imagePath = test.addScreenCapture(imgFilename); test.log(LogStatus.FAIL, "Added image to report", imagePath); 

这将在ExtentReports文件夹中创建报告和图像,但是当打开报告并检查(空白)图像时,将鼠标hover在图像src上会显示“无法加载图像”src =“。\ ExtentReports \ QXKmoVZMW7.png”。

这可以通过使用系统属性“user.dir”为图像的相对路径和文件名加前缀来解决。 所以这很好用,图像出现在html报告中。

克里斯

 String imgFilename = GenericMethods.takeScreenShot(driver,System.getProperty("user.dir") + "\\" + outputFolder); String imagePath = test.addScreenCapture(imgFilename); test.log(LogStatus.FAIL, "Added image to report", imagePath); 

Spring Boot的示例。 我的WSDL文件位于“wsdl”文件夹的Resources中。 WSDL文件的路径是:

资源/ WSDL / WebServiceFile.wsdl

要获取从某个方法到此文件的路径,您可以执行以下操作:

 String pathToWsdl = this.getClass().getClassLoader(). getResource("wsdl\\WebServiceFile.wsdl").toString();