Java Properties list() Method



Description

The Java Properties list(PrintStream out) method prints this property list out to the specified output stream. This method is useful for debugging.

Declaration

Following is the declaration for java.util.Properties.list() method

public void list(PrintStream out)

Parameters

out − an output stream.

Return Value

This method does not return a value.

Exception

ClassCastException − if any key in this property list is not a string.

Java Properties list(PrintWriter out) Method

Description

The java.util.Properties.list(PrintWriter out) method prints this property list out to the specified output stream. This method is useful for debugging.

Declaration

Following is the declaration for java.util.Properties.list() method

public void list(PrintWriter out)

Parameters

out − an output stream.

Return Value

This method does not return a value.

Exception

ClassCastException − if any key in this property list is not a string.

Listing Key-Value pairs of Properties to Console Example

The following example shows the usage of Java Properties list(PrintStream) method to print the properties to a stream. We're using System.out as PrintStream.

package com.tutorialspoint;

import java.util.Properties;

public class PropertiesDemo {
   public static void main(String[] args) {
      Properties prop = new Properties();

      // add some properties
      prop.put("Height", "200");
      prop.put("Width", "150");
      prop.put("Scannable", "true");

      // print the list with System.out
      prop.list(System.out);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

-- listing properties --
Width = 150
Height = 200
Scannable = true

Listing Key-Value pairs of Properties to Console Using PrintWrite Example

The following example shows the usage of Java Properties list(PrintWriter) method to print the properties to a writer object. We're using System.out as PrintWriter.

package com.tutorialspoint;

import java.io.PrintWriter;
import java.util.Properties;

public class PropertiesDemo {
   public static void main(String[] args) {
      Properties prop = new Properties();
      PrintWriter writer = new PrintWriter(System.out);

      // add some properties
      prop.put("Height", "200");
      prop.put("Width", "150");
      prop.put("Scannable", "true");

      // print the list with a PrintWriter object
      prop.list(writer);

      // flush the stream
      writer.flush();
   }
}

Output

Let us compile and run the above program, this will produce the following result −

-- listing properties --
Width = 150
Height = 200
Scannable = true
java_util_properties.htm
Advertisements