为什么print()方法的printwriter类不打印字符串? [英] Why print() method of printwriter class does not print the string ?

查看:193
本文介绍了为什么print()方法的printwriter类不打印字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 public class i4 
{
public static void main(String [] args)
{
PrintStream z = new PrintStream(System.out);
z.print(这是印刷流);
z.println(hi);

PrintWriter c = new PrintWriter(System.out,true);
c.println(zxc);
c.print(你好); }

}





使用PrintStream时,两个方法都打印字符串但是对于PrintWriter print()方法不打印hello。为什么?



我尝试了什么:



我试过这个关于netbeans的程序。

解决方案

查看文档:PrintWriter(Java Platform SE 7) [ ^ ]

非常清楚地说:

与PrintStream类,如果启用了自动刷新,则只有在调用println,printf或format方法之一时才会执行,而不是每当输出换行符时。这些方法使用平台自己的行分隔符概念而不是换行符。



因此,如果你不使用println,输出就不会发生任何事情。


public class i4
{
    public static void main(String[] args) 
    {
    PrintStream  z =new PrintStream (System.out) ;    
   z.print ( " this is print stream ") ;  
    z.println ( " hi") ;
   
   PrintWriter c = new PrintWriter (System.out , true) ;
   c.println ( " zxc ") ;
   c.print ( " hello  ") ;    }
    
}



When using PrintStream both the methods print the strings but for PrintWriter print() method does not print hello . Why ?

What I have tried:

I have tried this program on netbeans .

解决方案

Look at the documentation: PrintWriter (Java Platform SE 7 )[^]
It says very clearly:

Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked, rather than whenever a newline character happens to be output. These methods use the platform's own notion of line separator rather than the newline character.


So if you don't use println, nothing will happen to the output.


这篇关于为什么print()方法的printwriter类不打印字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆