Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.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_Classpath - Fatal编程技术网

Java:在类路径上加载和编辑属性文件

Java:在类路径上加载和编辑属性文件,java,properties,classpath,Java,Properties,Classpath,我想将一些属性保留到属性文件中,以防它们在程序运行期间被更改。现在我正试图这样做: Properties properties = new Properties(); try (FileInputStream in = new FileInputStream("classpath:app.properties")) { properties.load(in); } catch (IOException e) { logger.error("",

我想将一些属性保留到属性文件中,以防它们在程序运行期间被更改。现在我正试图这样做:

Properties properties = new Properties();

    try (FileInputStream in = new FileInputStream("classpath:app.properties")) {
        properties.load(in);
    } catch (IOException e) {
        logger.error("", e);
    }

    properties.setProperty("word", "abc");

    try (FileOutputStream out = new FileOutputStream("classpath:app.properties")) {
       properties.store(out, null);
    } catch (IOException e) {
        logger.error("", e);
    }

但它似乎不起作用。我做错了什么?

如果文件与程序位于同一目录中,则不需要类路径部分

Properties properties = new Properties();

try (FileInputStream in = new FileInputStream("app.properties")) {
    properties.load(in);
} catch (IOException e) {
    logger.error("", e);
}

properties.setProperty("word", "abc");

try (FileOutputStream out = new FileOutputStream("app.properties")) {
   properties.store(out, null);
} catch (IOException e) {
    logger.error("", e);
}

否则,如果文件在jar中,则必须重新打包jar。如果文件与程序位于同一目录中,则不需要类路径部分

Properties properties = new Properties();

try (FileInputStream in = new FileInputStream("app.properties")) {
    properties.load(in);
} catch (IOException e) {
    logger.error("", e);
}

properties.setProperty("word", "abc");

try (FileOutputStream out = new FileOutputStream("app.properties")) {
   properties.store(out, null);
} catch (IOException e) {
    logger.error("", e);
}

否则,如果文件在您的jar中,您必须重新打包jar才能读取和写入属性文件,这些属性文件位于
resources
文件夹中(在标准maven项目结构中),您可以使用以下方法:

// of course, do not forget to close the stream, after your properties file has been read.
properties.load(Runner.class.getClassLoader().getResourceAsStream("app.properties"));
修改属性文件后,可以使用以下内容:

Runner.class.getClassLoader().getResource("app.properties").getFile()
获取文件的绝对路径


顺便说一下,只是为了确保你检查的文件是正确的。您不应该检查
resources
文件夹中的文件。修改后的文件将与您的类一起放在根文件夹中,如
target
out
,以便能够读取和写入属性文件,这些属性文件位于
资源
文件夹中(在标准maven项目结构中),您可以使用以下方法:

// of course, do not forget to close the stream, after your properties file has been read.
properties.load(Runner.class.getClassLoader().getResourceAsStream("app.properties"));
修改属性文件后,可以使用以下内容:

Runner.class.getClassLoader().getResource("app.properties").getFile()
获取文件的绝对路径


顺便说一下,只是为了确保你检查的文件是正确的。您不应该检查
resources
文件夹中的文件。修改后的文件将与您的类一起放在根文件夹中,如
target
out

如果您想通过类路径坚持属性文件处理-您当然可以按原样使用下面的代码,我认为这是在类路径中处理文件的最简单方法,用于读写目的

try{

    InputStream in =   this.getClass().getResourceAsStream("/suggest.properties");
    Properties props = new Properties();
    props.load(in);
    in.close();


    URL url = this.getClass().getResource("/suggest.properties");
    File fileObject = new File(url.toURI());

    FileOutputStream out = new FileOutputStream(fileObject);

    props.setProperty("country", "america");
    props.store(out, null);
    out.close();

    }catch(Exception e)
    {
        System.out.println(e.getMessage());
    }

如果您想坚持通过类路径处理属性文件,您当然可以使用下面的代码,我认为这是在类路径中处理文件的最简单方法,用于读写目的

try{

    InputStream in =   this.getClass().getResourceAsStream("/suggest.properties");
    Properties props = new Properties();
    props.load(in);
    in.close();


    URL url = this.getClass().getResource("/suggest.properties");
    File fileObject = new File(url.toURI());

    FileOutputStream out = new FileOutputStream(fileObject);

    props.setProperty("country", "america");
    props.store(out, null);
    out.close();

    }catch(Exception e)
    {
        System.out.println(e.getMessage());
    }

我也试着这么做,发现我的属性实际上没有加载。我无法获取文件中定义的道具。我也尝试了同样的方法,但发现我的属性没有实际加载。我无法获取文件中定义的道具。