回车(CR)与换行(LF)

一、“回车”(Carriage Return)和“换行”(Line Feed)的来历

 

      在计算机还没有出现之前,有一种叫做电传打字机(Teletype Model 33,Linux/Unix下的tty概念也来自于此)的玩意,每秒钟可以打10个字符。但是它有一个问题,就是打完一行换行的时候,要用去0.2秒,正好可以打两个字符。要是在这0.2秒里面,又有新的字符传过来,那么这个字符将丢失。

      于是,研制人员想了个办法解决这个问题,就是在每行后面加两个表示结束的字符。一个叫做“回车”,告诉打字机把打印头定位在左边界;另一个叫做“换行”,告诉打字机把纸向下移一行。这就是“换行”和“回车”的来历。

 

二、计算机里的“回车”与“换行”

请看下面这段程序:
public class CRLFTest {

    public static void main(String[] args) {
        // 1. 中间包含\n
        String str = new String("Hello, World! \nThis is new line.");
        byte[] bytes = str.getBytes();

        System.out.println(str);
        for (int i = 0; i < bytes.length; i++) {
            System.out.print(bytes[i] + " ");
        }
        System.out.println();

        // 2. 中间包含\n\r
        str = new String("Hello, World! \n\rThis is new line.");
        bytes = str.getBytes();

        System.out.println(str);
        for (int i = 0; i < bytes.length; i++) {
            System.out.print(bytes[i] + " ");
        }
        System.out.println();

        // 3. 中间包含\r
        str = new String("Hello, World! \rThis is new line.");
        bytes = str.getBytes();

        System.out.println(str);
        for (int i = 0; i < bytes.length; i++) {
            System.out.print(bytes[i] + " ");
        }
        System.out.println(new Date());
    }
}
 以下是该程序在Windows, Linux, Mac三种环境下的运行结果:
1、Windows下的运行结果:
Hello, World! 
This is new line.
72 101 108 108 111 44 32 87 111 114 108 100 33 32 10 84 104 105 115 32 105 115 32 110 101 119 32 108 105 110 101 46 
Hello, World! 

This is new line.
72 101 108 108 111 44 32 87 111 114 108 100 33 32 10 13 84 104 105 115 32 105 115 32 110 101 119 32 108 105 110 101 46 
Hello, World! 
This is new line.
72 101 108 108 111 44 32 87 111 114 108 100 33 32 13 84 104 105 115 32 105 115 32 110 101 119 32 108 105 110 101 46 Wed May 25 11:28:42 CST 2011
 2、Linux下的运行结果:
Hello, World! 
This is new line.
72 101 108 108 111 44 32 87 111 114 108 100 33 32 10 84 104 105 115 32 105 115 32 110 101 119 32 108 105 110 101 46 
Hello, World! 
This is new line.
72 101 108 108 111 44 32 87 111 114 108 100 33 32 10 13 84 104 105 115 32 105 115 32 110 101 119 32 108 105 110 101 46 
This is new line.
72 101 108 108 111 44 32 87 111 114 108 100 33 32 13 84 104 105 115 32 105 115 32 110 101 119 32 108 105 110 101 46 
 3、Mac下的运行结果:
Hello, World! 
This is new line.
72 101 108 108 111 44 32 87 111 114 108 100 33 32 10 84 104 105 115 32 105 115 32 110 101 119 32 108 105 110 101 46 
Hello, World! 

This is new line.
72 101 108 108 111 44 32 87 111 114 108 100 33 32 10 13 84 104 105 115 32 105 115 32 110 101 119 32 108 105 110 101 46 
Hello, World! 
This is new line.
72 101 108 108 111 44 32 87 111 114 108 100 33 32 13 84 104 105 115 32 105 115 32 110 101 119 32 108 105 110 101 46 
 从上面的运行结果可以看出:
1、Windows与Mac的结果一致;
2、三种平台下,\n的实际结果是“回车” + “换行”;
3、Windows、Mac下的\r也是“回车” + “换行”,但是Linux下,\r却是回到当前行首,会抹到以前的输出结果;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值