码迷,mamicode.com
首页 > 其他好文 > 详细

关于System.out.println()与System.out.print("\n")的区别

时间:2015-07-22 06:50:45      阅读:441      评论:0      收藏:0      [点我收藏+]

标签:

这是在写junit测试的时候发现的。

 1 import java.io.ByteArrayOutputStream;
 2 import java.io.PrintStream;
 3 
 4 public class Test {
 5     public static void main(String[] args) {
 6         PrintStream out=System.out;
 7         ByteArrayOutputStream outContent = new ByteArrayOutputStream();
 8         System.setOut(new PrintStream(outContent));
 9         System.out.println("hello");
10         System.setOut(out);
11         System.out.println(outContent.toString().equals("hello\n"));
12         outContent.reset();
13         System.setOut(new PrintStream(outContent));
14         System.out.print("hello\n");
15         System.setOut(out);
16         System.out.println(outContent.toString().equals("hello\n"));
17     } 
18 }

上面这段程序输出的结果是false true。这意味着System.out.println()与System.out.print("\n")输出的字符还是有差别的。如果要将输出重定向到str用于测试是否相等的话,需要注意这一点。

关于System.out.println()与System.out.print("\n")的区别

标签:

原文地址:http://www.cnblogs.com/fuji/p/4666172.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!