Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 加载属性文件_Java_Properties_Filepath - Fatal编程技术网

Java 加载属性文件

Java 加载属性文件,java,properties,filepath,Java,Properties,Filepath,我有类似这样的代码,它工作得很好,但是如何呢 但是我想用System.getProperty(“user.dir”)替换绝对路径; 但这给了我一个带背景的字符串我怎么才能解决它, 或者替换它,以便将其转换为c:/ public static void main(String[] args) throws Exception{ String strPropertiePath=System.getProperty("user.dir"); System.out.

我有类似这样的代码,它工作得很好,但是如何呢 但是我想用System.getProperty(“user.dir”)替换绝对路径; 但这给了我一个带背景的字符串我怎么才能解决它, 或者替换它,以便将其转换为c:/

public static void main(String[] args) throws Exception{

         String strPropertiePath=System.getProperty("user.dir");
         System.out.println("strPropertiePath "+strPropertiePath);


         String absoluthPath2Propertie = "C:/Users/maurice/Dropbox/a_projectturkey/solution_06_09_2014/Application_Propertie/logging.properties";

     File fileLog = new File(absoluthPath2Propertie);
     LogManager.getLogManager().readConfiguration(new FileInputStream(absoluthPath2Propertie));
     //ConfigSystem.setup();

}

}

只需使用具有正确父子关系的
文件
路径
对象即可。您不需要关心斜杠和黑斜杠,
File
Path
将为您处理它们

例如,要定义一个属性文件,该文件位于
props
子文件夹的user dir文件夹中,并且具有文件名
myprops.properties
,您可以这样使用它:

File propFile = new File(System.getProperty("user.dir"),
    "/props/myprops.properties");
// Use try-with-resources to properly close the file input stream
try (InputStream in = new FileInputStream(propFile)) {
    LogManager.getLogManager().readConfiguration(in);
}
您可以按如下方式加载此属性文件:

File propFile = new File(System.getProperty("user.dir"),
    "/props/myprops.properties");
// Use try-with-resources to properly close the file input stream
try (InputStream in = new FileInputStream(propFile)) {
    LogManager.getLogManager().readConfiguration(in);
}
编辑:

因此,如果在用户目录中需要名为
logging.properties
的文件,只需使用以下命令:

File propFile = new File(System.getProperty("user.dir"),
    "logging.properties");
try (InputStream in = new FileInputStream(propFile)) {
    LogManager.getLogManager().readConfiguration(in);
}

您可以使用以下代码获取系统文件系统路径分隔符:

System.getProperties("file.separator")

通过使用此选项,您将能够在任何受支持的平台上创建正确的路径。这样,您可以在基于UNIX/Linux的系统中使用斜杠,在Windows上使用反斜杠。

使用以下代码加载属性文件

    Properties properties=new Properties();
    InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
    properties.load(in);

    properties.get("user.dir");

user.dir返回给我:C:\Users\maurice\Dropbox\a\U projectturkey\solution\U 06\U 09\U 2014\Application\U properties但我需要以下路径:C:/Users/maurice/Dropbox/a\U projectturkey/solution\U 06\U 09\U 2014/Application\U Propertie/logging.properties“解决方案可能很简单,但我不知道如何准备user.dir字符串,以便我得到:c://................/user.dir返回给我:c:\Users\maurice\Dropbox\a\u projectturkey\solution\u 06\u 09\u 2014\Application\u Propert‌​但我需要以下路径:C:/Users/maurice/Dropbox/a_projectturkey/solution_06_09_2014/Application_Propert‌​ie/logging.properties“解决方案可能很简单,但我不知道如何准备user.dir字符串,以便我得到:c:/…../../../../–编辑,添加您的特定案例。您不需要关心斜杠和黑色斜杠,
文件将为您处理。