有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

    我会尝试以下方法:

    加载源属性文件:

    Properties p = new Properties();
    try (InputStream i = new FileInputStream("source.properties")) {
        p.load(i);
    }
    

    删除目标文件中不需要的属性:

    p.remove("propertyKey");
    

    或者,您也可以设置一个新的Properties对象,并在那里复制属性:

    Properties p2 = new Properties();
    p2.setProperty("key", p.getProperty("key"));
    

    最后写出新的属性文件:

    try (OutputStream o = new FileOutputStream("dest.properties")) {
        p.store(o, "some comments");
        // or p2.store(o, "some comments");
    }
    

    在这种情况下,如果你的source.properties中有这个:

    a=b\nc
    

    你将在dest.properties中得到同样的结果:

    a=b\nc