System.in.read(); System.out.write();

The java.io.InputStream class is the abstract superclass for all input streams. It declares the three basic methods needed to read bytes of data from a stream. It also has methods for closing and flushing streams, checking how many bytes of data are available to be read, skipping over input, marking a position in a stream and resetting back to that position, and determining whether marking and resetting are supported.
public abstract int read() throws IOException
public int read(byte[] data) throws IOException
public int read(byte[] data, int offset, int length) throws IOException
public long skip(long n) throws IOException
public int available() throws IOException
public void close() throws IOException
public synchronized void mark(int readlimit)
public synchronized void reset() throws IOException
public boolean markSupported()

System.in是InputStream的子类的一个实例,其具有以上方法。System.in.read();是从缓冲区中读取下一字节的数据,然后返回的是该数据的ASCII码。


The java.io.OutputStream class declares the three basic methods you need to write bytes of data onto a stream. It also has methods for closing and flushing streams.
public abstract void write(int b) throws IOException
public void write(byte[] data) throws IOException
public void write(byte[] data, int offset, int length) throws IOException
public void flush() throws IOException
public void close() throws IOException

System.out是OutputStream的子类的一个实例,其具有以上方法。System.out.write();是向缓冲区写入字节,其和System.out.println();是不同的。

System.out.write(n); System.out.println(n);

前者输出ASCII码为整数n所对应的字符(write()是写入字节流),后者直接输出整数n(print()是写入字符流)。

如何输入1并按回车,结果如下:

1
49
49
11
End of loop 0!
13
13


End of loop 1!
10
10



End of loop 2!

注意"回车"(键盘上的enter)会被解释为/r和/n两个字符,分别表示为回车和换行,这两个字符的来历还挺有意思的。

现在关于在缓冲区内二进制数据的存取形式还是存在很大的疑惑,希望以后可以解决。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值