store(Writer, String): This method is available in java.util.Properties class of Java.
Syntax:
void java.util.Properties.store(Writer writer, String comments) throws IOException
This method takes two arguments. This method writes this property list (key and element pairs) in this Properties table to the output character stream in a format suitable for using the load (Reader) method.
Parameters: Two parameters are required for this method.
writer: an output character stream writer.
comments: a description of the property list.
Throws:
1. IOException - if writing this property list to the specified output stream throws an IOException.
2. ClassCastException - if this Properties object contains any keys or values that are not Strings.
3. NullPointerException - if the writer is null.
Approach 1: When no exception
Java
import java.io.IOException;import java.io.StringWriter;import java.io.Writer;import java.util.Properties;public class Propertiesstore2 {public static void main(String[] args) throws IOException {Properties properties = new Properties();properties.put("Hello", "World");properties.put("C++", "Program");properties.put("Java", "Program");Writer writer = new StringWriter();properties.store(writer, null);System.out.println(writer.toString());}}
Output:
#Sat Mar 19 15:09:19 IST 2022
Java=Program
C++=Program
Hello=World
Approach 2: NullPointerException
Java
import java.io.IOException;import java.io.Writer;import java.util.Properties;public class Propertiesstore2 {public static void main(String[] args) throws IOException {Properties properties = new Properties();properties.put("Hello", "World");properties.put("C++", "Program");properties.put("Java", "Program");Writer writer = null;properties.store(writer, null);System.out.println(properties);}}
Output:
Exception in thread "main" java.lang.NullPointerException at java.base/java.io.Writer.<init>(Writer.java:174) at java.base/java.io.BufferedWriter.<init>(BufferedWriter.java:95) at java.base/java.io.BufferedWriter.<init>(BufferedWriter.java:82) at java.base/java.util.Properties.store(Properties.java:869)
No comments:
Post a Comment