如何在java中定义相对路径
以下是我的项目结构: 我需要在如何在java中定义相对路径,java,relative-path,jdk1.6,Java,Relative Path,Jdk1.6,以下是我的项目结构: 我需要在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
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(系统找不到指定的文件)
如何在Java中定义相对路径?我正在使用JDK1.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");
试图访问文件的路径位于项目目录中,然后像这样访问文件
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
指向(windows)
而不是
C:\Directory
您也可以使用Path path1=FileSystems.getDefault().getPath(System.getProperty(“user.home”)、“downloads”、“somefile.txt”)我在使用图像文件的相对路径将屏幕截图附加到ExtentReports时遇到问题。执行时我的当前目录是“C:\Eclipse 64位\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文件夹中创建报告和图像,但当打开报告并检查(空白)图像时,将鼠标悬停在图像src上会显示“无法加载图像”src=“。\ExtentReports\QXKmoVZMW7.png”
这可以通过在映像的相对路径和文件名前面加上系统属性“user.dir”来解决。因此,这非常有效,图像显示在html报告中
克里斯
首先,看看绝对路径和相对路径之间的区别: 绝对路径始终包含根元素和定位文件所需的完整目录列表 或者,为了访问文件,需要将相对路径与另一路径组合 在构造函数文件(字符串路径名)中,Javadoc的File类表示 路径名(无论是抽象的还是字符串形式的)可以是绝对的,也可以是相对的 如果要获取相对路径,必须定义从当前工作目录到文件或目录的路径。请尝试使用以获取此路径。如您绘制的图片所示:
String localDir = System.getProperty("user.dir");
File file = new File(localDir + "\\config.properties");
此外,应尽量避免使用类似的“.”、“/”和其他类似的相对于文件位置的相对路径,因为移动文件时,更难处理 弹簧靴的示例。我的WSDL文件位于“WSDL”文件夹中的参考资料中。WSDL文件的路径为: resources/wsdl/WebServiceFile.wsdl 要获取从某个方法到此文件的路径,可以执行以下操作:
String pathToWsdl = this.getClass().getClassLoader().
getResource("wsdl\\WebServiceFile.wsdl").toString();
这是Windows中的有效相对路径。。但你不在你所处的位置。(提示,它是相对于当前工作目录而不是源文件的。)您应该将
config.properties
保存在src
下。在src
下创建config
包,在config
包下保留config.properties
。并以config/config.properties
的形式访问它。config.properties
会随Jar一起分发吗?如果是这样,它就变成了一个不能被文件访问的文件,但必须通过URL
访问。是的,它与jar文件一起分发。我如何在这里写入相对路径?@ishk我不知道java是否支持通过包的相对访问,我们可以像上面所说的那样从项目目录访问文件,并访问子文件夹。如文件f=new File(“folder/filename.txt”);使用新文件(“/path/to/File”).getAbsolutePath()不是更好吗?这样,输入可以是相对的,也可以是绝对的,输出将始终是绝对的。@cmc什么是“path/to/file”值?与“path to the property file”相同。@cmc True。!!更新了我的答案以合并相同的内容。以这种方式创建文件(“”
)时,参数应该是项目中文件的路径,而不是”
。
String imgFilename = GenericMethods.takeScreenShot(driver,System.getProperty("user.dir") + "\\" + outputFolder);
String imagePath = test.addScreenCapture(imgFilename);
test.log(LogStatus.FAIL, "Added image to report", imagePath);
String localDir = System.getProperty("user.dir");
File file = new File(localDir + "\\config.properties");
String pathToWsdl = this.getClass().getClassLoader().
getResource("wsdl\\WebServiceFile.wsdl").toString();