zoukankan      html  css  js  c++  java
  • PrintWriter write与println方法的区别

        PrintWriter在以下以pw代替,在写client与server进行测试的通讯程序时,用pw.println(str)可以把数据发送给客户端,而pw.write(str)却不行!
    查看源码发现:
        pw.println(str)方法是由write方法与println()方法组成,页println()方法中执行了newLine()方法。
       而 newLine()实现中有一条out.write(lineSeparator);
        即println(str)方法比write方法中多输出了一个lineSeparator字符; 
       其中lineSeparator实现为;lineSeparator = (String) java.security.AccessController.doPrivileged(new sun.security.action.GetPropertyAction("line.separator"));
       而line.separator属性跟据每个系统又是不一样的。
       println()方法的注释说明中提到:
      /**
       * Terminates the current line by writing the line separator string.  The
       * line separator string is defined by the system property
       * <code>line.separator</code>, and is not necessarily a single newline
       * character (<code>'\n'</code>).
       */
       ----------------

    上述引用他人之手,重点在于不是简单的加上\r\n 之类的转义来替换 上述提到的lineSeparator

    如果一定要用write(),就必须使用write()+println()的组合。

    当然给flush()的还是要flush.除非你在构造的时候就已经对autoFlush进行了初始化为true的操作

    感兴趣的朋友可以在看看这里:http://www.oschina.net/question/101123_17855

  • 相关阅读:
    Cassandra
    POST和GET方法
    webx流程
    Cassandra的内部数据存储结构
    Session和Cookie
    昆爷又发了4篇siggraph2010,牛A...榜样和目标...
    真正开博了
    ebook搜索,I can! cool!
    (转)计算机图形学的学习
    3D图形学习的现在和将来(转)
  • 原文地址:https://www.cnblogs.com/draem0507/p/3065588.html
Copyright © 2011-2022 走看看