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
![](http://www.cs.utk.edu/~pham/ascii_table.jpg)
/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
相关文章推荐
- ByteArrayInputStream 和InputStream中read方法
- 【Java基础知识】IO流--内存操作流ByteArrayInputStream、ByteArrayOutputStream
- ByteArrayInputStream和ByteArrayOutputStream类
- struts2下载文件时出错提示:java.lang.ClassCastException: java.io.ByteArrayInputStream cannot be cast to java.l
- 达内课程-ByteArrayInputStream/ByteArrayOutputStream
- 内存流操作ByteArrayInputStream,ByteArrayOutputStream
- ByteArrayOutputStream和ByteArrayInputStream详解
- java.ByteArrayInputStream与ByteArrayOutputStream浅谈
- ByteArrayOutputStream和ByteArrayInputStream详解
- blob 读取转为 ByteArrayInputStream
- Java基础---ByteArrayInputStream、ByteArrayOutputStream
- 转存:JAVA IO - ByteArrayInputStream和BufferedInputStream的区别
- java.ByteArrayInputStream-ByteArrayOutputStream
- java的BytearrayInputStream和BytearrayOutputStream的讲解
- Java IO - ByteArrayInputStream&ByteArrayOutputStream
- Java IO:常见Java IO流介绍(ByteArrayInputStream,ObjectInputStream,BufferedInputStream)
- Java IO流学习总结六:ByteArrayInputStream、ByteArrayOutputStream
- 黑马程序员_java_IO流_FileInputStream_FileOutputStream_File_递归_Properties_ByteArrayInputStream_ByteArrayOut
- 在java开发过程中什么时候使用ByteArrayInputStream和ByteArrayOuitputStream?
- java IO流分析:ByteArrayInputstream 和 BufferedInputStream