JavaSE---Basic I/O

1、I/O Streams

    1.1、Byte Streams

        Programs use byte streams to perform input and output of 8-bit bytes. (程序使用 字节流 输入|输出 8位字节)

        All byte stream classes are descended from InputStream and OutputStream.(所有的字节流 用inputstream | outputstream表示)

        CopyBytes example spends most of its time in a simple loop that reads the input stream and writes the output stream, one byte at a time.(字节流案例 花费大量的时间在 循环 读取、写入 【一次一字节】)

        CopyBytes seems like a normal program, but it actually represents a kind of low-level I/O that you should avoid. Since xanadu.txt contains character data, the best approach is to use character streams.(字节流 是一种低等级的IO,文件xanadu.txt是一个字符数据文件,可以使用 character streams)

    1.2、Character Streams

        All character stream classes are descended from Reader and Writer.(所有的字符流 用Reader、Wirter表示)

        Line-Oriented I/O(行读)

          The CopyLines example invokes BufferedReader.readLine and PrintWriter.println to do input and output one line at a time.

    1.3、Buffered Streams

        Unbuffered I/O means each read or write request is handled directly by the underlying OS.(非缓存流IO 意味着 每次读写 请求 都被底层OS直接处理)

        This can make a program much less efficient, since each such request often triggers disk access, network activity, or some other operation that is relatively expensive.(非缓存流 使得程序非常低效,因为每个请求 都触发磁盘访问、网络活动、其他的操作)

        To reduce this kind of overhead, the Java platform implements buffered I/O streams.(为了减少 非缓存流,Java平台提供了 缓存流)

        Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty. Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.(缓存输入流 从内存的缓存读取数据 直到 缓存为空;缓存输出流 写数据到缓存中 直到缓存满)

        A program can convert an unbuffered stream into a buffered stream using the wrapping idiom.(一个程序可以 将非缓存流转换成缓存流 使用包装类)

          eg:

            BufferedInputStream and BufferedOutputStream create buffered byte streams.

            BufferedReader and BufferedWriter create buffered character streams.

    1.4、Scanning and Formatting

        Programming I/O often involves translating to and from the neatly formatted data humans like to work with.(程序IO 通常涉及翻译数据 至人类可以工作的格式)

        The Java platform provides two APIs. The scanner API breaks input into individual tokens associated with bits of data. The formatting API assembles data into nicely formatted, human-readable form.(Java平台提供了Scanner:将大量数据拆分成 独立字符;Formatting:将拆分的数据组合)

        Scanning

          By default, a scanner uses white space to separate tokens.(Scanner默认使用 空白 作为分隔符)

          To use a different token separator, invoke useDelimiter(), specifying a regular expression.(可以使用userDelimiter()自定义分隔符)

        Formatting

    1.5、I/O from the Command Line

        The Java platform supports this kind of interaction in two ways: through the Standard Streams and through the Console.(Java平台支持2种类型的交互:标准流、控制台)

          标准流:System.in; System.out;System.err;

          控制台:System.console().

上一篇:03-K8S Basic-kubernetes应用快速入门


下一篇:HTTP Basic 认证