0

I'm new to Java and is trying to learn the class Properties. I have came up with the code below. I wanted to print the "keys" and "values" stored in Properties variable p2 using different methods. However, the code

PrintWriter pw1 = new PrintWriter(System.out);
p2.list(pw1);

Doesn't seem to be printing out anything. Why is this happening? Could someone please help me out? Thanks in advance for any help!

Properties p1 = new Properties();

try (OutputStream os1 = new FileOutputStream("whateverAmericaFile2.txt")){
    p1.setProperty("1", "one");
    p1.setProperty("2", "two");
    p1.setProperty("3", "three");
    p1.store(os1, "comment");
} catch(IOException e){
    e.printStackTrace();
}

Properties p2 = new Properties();
try (InputStream is1 = new FileInputStream("whateverAmericaFile2.txt")){
    p2.load(is1);
    System.out.println(p2.getProperty("2"));
} catch (IOException e){
    e.printStackTrace();
}

System.out.println("before PrintWriter");

PrintWriter pw1 = new PrintWriter(System.out);
p2.list(pw1);


System.out.println("After PrintWriter, before Enumeration ");

Enumeration<Object> eo1 = p2.elements();
while (eo1.hasMoreElements()){
    System.out.println(eo1.nextElement());
}

System.out.println("after Enumeration");

}

1 Answer 1

2

You have to call pw1.flush() after p2.list(pw1) in order to write the cached text to the console.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.