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

た 入场券 2022-08-18 02:19 231阅读 0赞

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()是写入字符流)。

java_01.jpg

如何输入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 条评论,231人围观)

还没有评论,来说两句吧...

相关阅读