socket通讯测试程序时遇到的PrintWriter write与println方法的区别
来源:互联网 发布:linux ssh 端口 编辑:程序博客网 时间:2024/05/16 10:42
我也是在做socket的C/S测试的时候发现这个问题的(使用write的时候并没有互相进行通信,但是换成println之后就可以了)。
那么就来看看write和println到底有啥区别吧,先看源码:
println(String x):
/** * Prints a String and then terminates the line. This method behaves as * though it invokes <code>{@link #print(String)}</code> and then * <code>{@link #println()}</code>. * * @param x the <code>String</code> value to be printed */ public void println(String x) { synchronized (lock) { print(x); println(); } }
可见,println()方法,在print()之后输出了一个新的一行,再看看print()干了些啥:
/** * Prints a string. If the argument is <code>null</code> then the string * <code>"null"</code> is printed. Otherwise, the string's characters are * converted into bytes according to the platform's default character * encoding, and these bytes are written in exactly the manner of the * <code>{@link #write(int)}</code> method. * * @param s The <code>String</code> to be printed */ public void print(String s) { if (s == null) { s = "null"; } write(s); }
再看看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>). */ public void println() { newLine(); }
newLine():
private void newLine() { try { synchronized (lock) { ensureOpen(); out.write(lineSeparator); if (autoFlush) out.flush(); } } catch (InterruptedIOException x) { Thread.currentThread().interrupt(); } catch (IOException x) { trouble = true; } }
所以,也不用看write()方法了,println()方法就是write()+另起一行。
如果只是区别,如上,欧了。
那么,为什么在我的socket测试里面,一定要用println方法呢?
就请听下回分解吧!啊哈哈
ref:
http://www.cnblogs.com/draem0507/archive/2013/05/07/3065588.html
http://www.oschina.net/question/101123_17855
0 0
- socket通讯测试程序时遇到的PrintWriter write与println方法的区别
- PrintWriter write与println方法的区别
- PrintWriter中write与println方法的区别
- PrintWriter中write与println方法的区别
- 关于PrintWriter的println()和write()方法区别
- PrintWriter 的Write()+flush()和println()方法区别
- 关于PrintWriter中的print与println方法的区别
- PrintWriter 的Write()方法和println()方法有何细微的区别
- Socket PrintWriter 中 write() 与 print() 的区别
- printWriter的write(String x)和println(String x)区别分析
- Java中PrintWriter类的println方法。
- 简述PrintWriter中write()方法和print()方法的区别?
- PrintWriter中write和print方法的区别
- java.io.PrintWriter 中 write() 与 print() 的区别
- java.io.PrintWriter 中 write() 与 print() 的区别
- java.io.PrintWriter 中 write() 与 print() 的区别
- 输出流中方法out.write与out.println的区别
- System.out.write与System.out.println的区别
- 数学符号读音
- On the Android ListView listSelector to solve cross-border issues
- Download模块 (十一)
- Python正则表达式
- UISegmentedControl的详细使用
- socket通讯测试程序时遇到的PrintWriter write与println方法的区别
- 第十七周项目4-日期结构体
- 听见我们内心的情绪
- BZOJ-3173-最长上升子序列
- C语言生成程序问题
- springMVC上传文件(11)
- android View各属性详解
- UVA - 1481 Genome Evolution 公式+二分
- LCA在线算法ST算法