Properties load(Reader) in Java

load(Reader): This method is available in java.util.Properties class of Java.

Syntax:

void java.util.Properties.load(Reader reader) throws IOException

This method takes one argument. This method reads a property list (key and element pairs) from the input character stream in a simple line-oriented format.

Note: Properties are processed in terms of lines.

Parameters: One parameter is required for this method.

reader: the input character stream.

Throws:

1. IOException - if an error occurred when reading from the input stream.

2. IllegalArgumentException - if a malformed Unicode escape appears in the input.

3. NullPointerException - if the reader is null.

Approach 1: When no exception

Java

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.Properties;

public class Propertiesload2 {
    public static void main(String[] args) throws IOException {

        Properties properties = new Properties();

        String s = "C++, Java, Python";

        Reader reader = new StringReader(s);

        properties.load(reader);
        properties.list(System.out);

    }
}

Output:

-- listing properties --

C++,=Java, Python


Approach 2: NullPointerException 

Java  

import java.io.IOException;
import java.io.Reader;
import java.util.Properties;

public class Propertiesload2 {
    public static void main(String[] args) throws IOException {

        Properties properties = new Properties();

        Reader reader = null;

        properties.load(reader);
        properties.list(System.out);

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: reader parameter is null at java.base/java.util.Objects.requireNonNull(Objects.java:233) at java.base/java.util.Properties.load(Properties.java:381)


No comments:

Post a Comment