Java 日看一类(10)之IO包中的DataInput接口

DataInput接口没有引入的包或继承的类;

接口注释如下:

/**
 * The {@code DataInput} interface provides
 * for reading bytes from a binary stream and
 * reconstructing from them data in any of
 * the Java primitive types. There is also
 * a
 * facility for reconstructing a {@code String}
 * from data in
 * <a href="#modified-utf-8">modified UTF-8</a>
 * format.
 * <p>
 * It is generally true of all the reading
 * routines in this interface that if end of
 * file is reached before the desired number
 * of bytes has been read, an {@code EOFException}
 * (which is a kind of {@code IOException})
 * is thrown. If any byte cannot be read for
 * any reason other than end of file, an {@code IOException}
 * other than {@code EOFException} is
 * thrown. In particular, an {@code IOException}
 * may be thrown if the input stream has been
 * closed.
 *
 * <h3><a name="modified-utf-8">Modified UTF-8</a></h3>
 * <p>
 * Implementations of the DataInput and DataOutput interfaces represent
 * Unicode strings in a format that is a slight modification of UTF-8.
 * (For information regarding the standard UTF-8 format, see section
 * <i>3.9 Unicode Encoding Forms</i> of <i>The Unicode Standard, Version
 * 4.0</i>).
 * Note that in the following table, the most significant bit appears in the
 * far left-hand column.
 *
 * <blockquote>
 *   <table border="1" cellspacing="0" cellpadding="8"
 *          summary="Bit values and bytes">
 *     <tr>
 *       <th colspan="9"><span style="font-weight:normal">
 *         All characters in the range {@code '\u005Cu0001'} to
 *         {@code '\u005Cu007F'} are represented by a single byte:</span></th>
 *     </tr>
 *     <tr>
 *       <td></td>
 *       <th colspan="8" id="bit_a">Bit Values</th>
 *     </tr>
 *     <tr>
 *       <th id="byte1_a">Byte 1</th>
 *       <td><center>0</center>
 *       <td colspan="7"><center>bits 6-0</center>
 *     </tr>
 *     <tr>
 *       <th colspan="9"><span style="font-weight:normal">
 *         The null character {@code '\u005Cu0000'} and characters
 *         in the range {@code '\u005Cu0080'} to {@code '\u005Cu07FF'} are
 *         represented by a pair of bytes:</span></th>
 *     </tr>
 *     <tr>
 *       <td></td>
 *       <th colspan="8" id="bit_b">Bit Values</th>
 *     </tr>
 *     <tr>
 *       <th id="byte1_b">Byte 1</th>
 *       <td><center>1</center>
 *       <td><center>1</center>
 *       <td><center>0</center>
 *       <td colspan="5"><center>bits 10-6</center>
 *     </tr>
 *     <tr>
 *       <th id="byte2_a">Byte 2</th>
 *       <td><center>1</center>
 *       <td><center>0</center>
 *       <td colspan="6"><center>bits 5-0</center>
 *     </tr>
 *     <tr>
 *       <th colspan="9"><span style="font-weight:normal">
 *         {@code char} values in the range {@code '\u005Cu0800'}
 *         to {@code '\u005CuFFFF'} are represented by three bytes:</span></th>
 *     </tr>
 *     <tr>
 *       <td></td>
 *       <th colspan="8"id="bit_c">Bit Values</th>
 *     </tr>
 *     <tr>
 *       <th id="byte1_c">Byte 1</th>
 *       <td><center>1</center>
 *       <td><center>1</center>
 *       <td><center>1</center>
 *       <td><center>0</center>
 *       <td colspan="4"><center>bits 15-12</center>
 *     </tr>
 *     <tr>
 *       <th id="byte2_b">Byte 2</th>
 *       <td><center>1</center>
 *       <td><center>0</center>
 *       <td colspan="6"><center>bits 11-6</center>
 *     </tr>
 *     <tr>
 *       <th id="byte3">Byte 3</th>
 *       <td><center>1</center>
 *       <td><center>0</center>
 *       <td colspan="6"><center>bits 5-0</center>
 *     </tr>
 *   </table>
 * </blockquote>
 * <p>
 * The differences between this format and the
 * standard UTF-8 format are the following:
 * <ul>
 * <li>The null byte {@code '\u005Cu0000'} is encoded in 2-byte format
 *     rather than 1-byte, so that the encoded strings never have
 *     embedded nulls.
 * <li>Only the 1-byte, 2-byte, and 3-byte formats are used.
 * <li><a href="../lang/Character.html#unicode">Supplementary characters</a>
 *     are represented in the form of surrogate pairs.
 * </ul>
 * @author  Frank Yellin
 * @see     java.io.DataInputStream
 * @see     java.io.DataOutput
 * @since   JDK1.0
 */

大意如下:

该接口用于从二进制流中读取字节,并根据的所有基本类型对其进行重构

同时也可以根据修改版UTF-8的格式重构字符串

如果在被请求的byte数目被读取之前就读取到文件末尾,一个文件结束事件(EOFException)将会被抛出

如果在文件结束之前由于各种原因不能读取文件,一个除了EOFException意外的IO异常将会被抛出

尤其当输入流关闭后将会抛出一个IO异常

修改版UTF-8:(注释里面插标签语言真是看的头疼)





本接口规定了15个方法:

传入缓冲区数组的readFully



传入缓冲区数组、读取指针、读取长度的readFully



跳过输入流后续n个字符的skipBytes




读入输入字符对其进行条件检测的readBoolean



读取输入流中单个字节(有符号返回



读取输入流中单个字节(无符号返回



读取输入流中两个字节(有符号返回



读取输入流中两个字节(无符号返回



读取两个字节并转化为字符类型



读取四个字节返回int值



读取八个字节返回long值



读取四个字节返回float值



读取8个字节返回double



读入单行文本


读入修改版UTF-8,返回字符串




说实话,接口难以去分析什么(毕竟没有源码,只能看看注释)闭嘴,看注释不如直接翻看文档尴尬,不过接口也必须要学习,有助理解许多完成该接口的类的功能。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: java.io內的函數有:InputStream、OutputStream、Reader、Writer、File、FileDescriptor、Serializable、DataInputDataOutput、Closeable、Flushable、FileFilter、FilenameFilter、FileInputStream、FileOutputStream、ObjectInputStream、ObjectOutputStream 以及 BufferedInputStream 等等。 ### 回答2: Java.ioJava编程语言提供的库之一,用于处理输入输出操作。在java.io包中含了许多和函数。以下是一些常用的函数: 1. File的函数: - createNewFile():创建一个新文件。 - delete():删除指定文件。 - exists():判断指定文件是否存在。 - getName():获取文件的名称。 - isDirectory():判断指定路径是否为目录。 - isFile():判断指定路径是否为文件。 - lastModified():获取文件最后修改的时间。 - length():获取文件的大小。 2. FileInputStream的函数: - read():读取一个字节的数据。 - read(byte[]):将读取的数据存入指定的字节数组。 - skip(long):跳过指定数量的字节。 - available():返回当前可读取的字节数。 - close():关闭输入流。 3. FileOutputStream的函数: - write(int):写入一个字节的数据。 - write(byte[]):将指定字节数组的数据写入文件。 - write(byte[], int, int):将指定字节数组的一部分写入文件。 - flush():刷新输出流缓冲区。 - close():关闭输出流。 4. BufferedReader的函数: - readLine():读取一行文本。 - read():读取一个字符的数据。 - skip(long):跳过指定数量的字符。 - ready():检查是否可以从输入流读取数据。 5. BufferedWriter的函数: - write(int):写入一个字符的数据。 - write(String):将指定的字符串写入文件。 - newLine():写入一个行分隔符。 - flush():刷新输出流缓冲区。 - close():关闭输出流。 上述只是一些常用的java.io的函数,还有许多其他方法和功能,可以根据具体需求进行使用。 ### 回答3: Java.ioJava编程语言用于处理输入和输出的标准。这个提供了一组用于读取和写入数据的接口。以下是java.io包中一些常用的函数: 1. File:用于操作文件和目录。常用函数括创建文件,删除文件,重命名文件,获取文件属性等。 2. FileInputStream和FileOutputStream:用于读取和写入二进制数据。常用函数括读取一个字节,读取多个字节,写入一个字节,写入多个字节等。 3. FileReader和FileWriter:用于读取和写入文本数据。常用函数括读取一个字符,读取多个字符,写入一个字符,写入多个字符等。 4. BufferedReader和BufferedWriter:用于缓冲读取和写入数据。常用函数括读取一行数据,写入一行数据等。 5. DataInputStream和DataOutputStream:用于读取和写入基本数据型。常用函数括读取整数,读取浮点数,写入整数,写入浮点数等。 6. ObjectInputStream和ObjectOutputStream:用于读取和写入对象。常用函数括读取对象,写入对象等。 7. InputStreamReader和OutputStreamWriter:用于将字节流转换成字符流。常用函数括读取一个字符,写入一个字符等。 8. ZipInputStream和ZipOutputStream:用于读取和写入ZIP格式的压缩文件。常用函数括解压缩文件,压缩文件等。 以上仅仅是java.io包中的一部分函数,这些函数的功能和用法非常丰富,可以根据具体的需求选择适合的函数来进行输入和输出操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值