以下是使用JAVA将属性保存到文件中的示例代码: ```java import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Properties; public class SavePropertiesToFile { public static void main(String[] args) { Properties properties = new Properties(); properties.setProperty("name", "John"); properties.setProperty("age", "30"); properties.setProperty("city", "New York"); try (OutputStream outputStream = new FileOutputStream("config.properties")) { properties.store(outputStream, "My Configuration"); } catch (IOException e) { e.printStackTrace(); } } } ``` 在此示例中,我们首先创建了一个Properties对象,并设置了三个属性:name,age和city。然后,我们使用FileOutputStream将属性保存到名为config.properties的文件中。最后,我们使用store方法将属性写入文件中,并提供了一个注释“我的配置”。 运行此代码后,将在项目目录中创建一个名为config.properties的文件,其中包含以下内容: ``` #My Configuration name=John age=30 city=New York ``` 这是一个标准的属性文件格式,其中每个属性都以“键=值”的形式表示,并且以“#”开头的行是注释。