java中\r,\n,\r\n,\n\r的区别,疑问?

本文深入探讨了Java中不同转义字符( , , , )在控制台输出时的行为差异,通过实例演示了这些字符在Windows和Unix系统中的换行效果,同时介绍了如何使用System.getProperty(line.separator)获取当前操作系统默认的换行符。

package com.test;

import java.io.IOException;
import java.io.InputStream;

public class Test {

	//java中\r,\n,\r\n,\n\r的区别,疑问?
	public static void main(String[] args) throws IOException{
		System.out.print("1----->\r5678test");
		System.out.print("2----->\n");
		System.out.print("3----->\n\r");
		System.out.print("4----->\r\n");
		System.out.print("5----->");
		System.out.print("yes");
		System.out.print("hello\r\n666");
		System.out.print("hi777");
		System.out.print("ok\n\r88");
		System.out.print("go1\n99");
		System.out.print("go2\r55");
		System.out.print("go3\r44");
		System.out.print("go4\n33");
		System.out.print("go5\n\r22");
		System.out.print("go6");
		System.out.print("aaa");
		// \f是换页符
		System.out.println("qwe\fabc");
//		使用System.getProperty("line.separator")来获取当前OS的换行符
		System.out.print(System.getProperty("line.separator"));
		testCarriageReturnAndNewLine();
	}
	
	public static void testCarriageReturnAndNewLine() throws IOException {
		InputStream in = System.in;
		StringBuilder sb = new StringBuilder();
		int ch = 0;
		while ((ch = in.read()) != -1) {
			if (ch == '\r') {
				System.out.println("\\r");
			}
			if (ch == '\n') {
				System.out.println("\\n");
			}
			sb.append((char) ch);
			System.out.println("数据有:" + sb.toString());
			System.out.println("长度为:" + sb.length());
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值