java '\r'和'\n' 问题

来源:百度知道 编辑:UC知道 时间:2024/05/05 23:42:06
public class ReadLine {
public static void main(String [] args)
{
byte buf[] = new byte[1024];
String strInfo = null;
int pos = 0;
int ch = 0;
System.out.println("please enter info,input bye for exit:");

while(true)
{
try
{
ch = System.in.read();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
switch(ch)
{
case '\r':
break;
//System.out.println('\r');
case '\n':
strInfo = new String(buf,0,pos);
if(strInfo.equals("bye"))
return;
else
System.out.println(strInfo);
pos = 0;
break;
default:
buf[pos++] = (byte)ch;
}
}
}
}
程序里面加入case '\r': break; 这句有什么

\r 回车
就是回到当前行的行首。

\n 回车并换行
另起一行,并回到另起一行的行首。

这是正则表达式里面的东西,随便粘了一点上来,希望你看得明白。

\t 制表符 ('\u0009')
\n 新行(换行)符 ('\u000A')
\r 回车符 ('\u000D')
\f 换页符 ('\u000C')
\a 报警 (bell) 符 ('\u0007')
\e 转义符 ('\u001B')
\cx 对应于 x 的控制符

字符类
[abc] a、b 或 c(简单类)
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围)
[a-d[m-p]] a 到 d 或 m 到 p:[a-dm-p](并集)
[a-z&&[def]] d、e 或 f(交集)
[a-z&&[^bc]] a 到 z,除了 b 和 c:[ad-z](减去)
[a-z&&[^m-p]] a 到 z,而非 m 到 p:[a-lq-z](减去)

预定义字符类
. 任何字符(与行结束符可能匹配也可能不匹配)
\d 数字:[0-9]
\D 非数字: [^0-9]
\s 空白字符:[ \t\n\x0B\f\r]
\S 非空白字符:[^\s]
\w 单词字符:[a-zA-Z_0-9]
\W 非单词字符:[^\w]

\r —— 换行。
\n —— 回车。
换行是把光标移到下一行的开头。
回车是把光标移到本行的开