您的位置:首页 > 其它

ByteArrayInputStream example

2010-06-01 14:47 459 查看
// example expression you want to evaluate in your program
String strExpression = "a = a++ + b;";
/*
* Here, while, evaluating the expression, You need to know whether it is ++ operator or + plus operator.
* Basically, you need to read ahead or peek whether another + follows immediately after one + sign.
*/
// get bytes from the expression string
byte bytes[] = strExpression.getBytes();
// create stream from expression bytes
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
/*
* PushbackInputStream provides below given two constructors. 1) PushbackInputStream(InputStream is) creates
* PushbackInputStream from provided InputStream object 2)PushbackInputStream(InputStream is, int size) Creates
* PushbackInputStream from specified InputStream object with pushback buffer of specified size.
*/
// create PushbackInputStream from ByteArrayInputStream
PushbackInputStream pis = new PushbackInputStream(bis);
int ch;
try
{
while ((ch = pis.read()) != -1)
{
// we encountered first + operator
// System.out.println(ch);
if (ch == '+')
{
// peek to see if another + follows
if ((ch = pis.read()) == '+')
{
System.out.print("Plus Plus");
}
else
{
// push back one position as we did not get another +
pis.unread(ch);
System.out.print("+");
}
}
else
{
// print the character
System.out.print((char) ch);
}
}
}
catch (IOException ioe)
{
System.out.println("Exception while reading" + ioe);
}
}


以上代码来自 http://www.java-examples.com/java-pushbackinputstream-example


/r在ASCII表中为13,/n为10

Carriage return
指回车,它最开始只是Baudot
code 的一个控制符,用于表示一行的结束,新行的开始,但不包含line feed


后来,它被用作打字机,用于将打字机的指针移到下一行的左边。ASCII表中为CR.更加详细的参看:http://en.wikipedia.org/wiki/Carriage_return

Line feed
即新行。ASCII表中为LF。

CRLF
, "carriage return [and]
line feed

不同操作系统对换行的要求:

LF
:
Multics
,
Unix
and Unix-like
systems (GNU
/Linux
, AIX
, Xenix
, Mac OS X
,
FreeBSD
,
etc.), BeOS
,
Amiga
, RISC OS
,
and others

CR
+LF
: DEC
RT-11
and
most other early non-Unix, non-IBM OSes, CP/M
, MP/M
, MS-DOS
, OS/2
, Microsoft Windows
, Symbian
OS

CR
:
Commodore
8-bit machines, TRS-80
, Apple II family
, Mac OS
up to version 9
and OS-9
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: