load(InputStream): This method is available in java.util.Properties class of Java.
Syntax:
void java.util.Properties.load(InputStream inStream) throws IOException
This method takes one argument. This method reads a property list (key and element pairs) from the input byte stream.
Parameters: One parameter is required for this method.
inStream: the input stream.
Throws:
1. IOException - if an error occurred when reading from the input stream.
2. IllegalArgumentException - if the input stream contains a malformed Unicode escape sequence.
3. NullPointerException - if inStream is null.
Approach 1: When no exception
Java
import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.Properties;public class Propertiesload {public static void main(String[] args) throws IOException {Properties properties = new Properties();String str = "C++ Programming Language";String str2 = "Java Programming Language";FileInputStream inStream =new FileInputStream("hello.txt");@SuppressWarnings("resource")FileOutputStream fileOutputStream =new FileOutputStream("hello.txt");fileOutputStream.write(str.getBytes());fileOutputStream.write("\n".getBytes());fileOutputStream.write(str2.getBytes());properties.load(inStream);properties.list(System.out);}}
Output:
-- listing properties --
Java=Programming Language
C++=Programming Language
Approach 2: NullPointerException
Java
import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.Properties;public class Propertiesload {public static void main(String[] args) throws IOException {Properties properties = new Properties();String str = "C++ Programming Language";String str2 = "Java Programming Language";FileInputStream inStream = null;@SuppressWarnings("resource")FileOutputStream fileOutputStream =new FileOutputStream("hello.txt");fileOutputStream.write(str.getBytes());fileOutputStream.write("\n".getBytes());fileOutputStream.write(str2.getBytes());properties.load(inStream);properties.list(System.out);}}
Output:
Exception in thread "main" java.lang.NullPointerException: inStream parameter is null at java.base/java.util.Objects.requireNonNull(Objects.java:233) at java.base/java.util.Properties.load(Properties.java:407)
No comments:
Post a Comment