简体   繁体   English

为什么Properties.list(PrintWriter out)不打印任何内容?

[英]why Properties.list(PrintWriter out) is not printing anything?

I'm new to Java and is trying to learn the class Properties. 我是Java的新手,正在尝试学习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. 我想使用不同的方法打印存储在Properties变量p2中的“键”和“值”。 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");

} }

您必须在p2.list(pw1) pw1.flush()之后调用pw1.flush()才能将缓存的文本写入控制台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM