import java.io.*; import java.util.Properties; public class PropertyFileWriter { public static void main(String[] args) throws IOException { Properties prop = new Properties(); prop.setProperty("database.url", "localhost:3306/mydb"); prop.setProperty("database.username", "myusername"); prop.setProperty("database.password", "mypassword"); FileOutputStream fos = new FileOutputStream("config.properties"); prop.store(fos, "Database Configuration"); } }
import java.io.*; import java.util.Properties; public class PropertyStreamWriter { public static void main(String[] args) throws IOException { Properties prop = new Properties(); prop.setProperty("database.url", "localhost:3306/mydb"); prop.setProperty("database.username", "myusername"); prop.setProperty("database.password", "mypassword"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); prop.store(baos, "Database Configuration"); String propertiesString = baos.toString(); System.out.println(propertiesString); } }This code writes the database configuration properties to a ByteArrayOutputStream and prints it to the console. The java.util package contains the Properties class.