Java教程 目录

Java DataOutputStream类

Java文件和输入和输出(I/O)

DataOutputStream类用于将原始数据类型写入输出源。以下是创建DataOutputStream的构造函数。

---------------- --- - ----------------------------- -----

当创建了DataOutputStream对象,就可以使用它的一些辅助方法来写入流或在流上执行其他操作。

编号 方法 描述
1 public final void write(byte[] w, int off, int len)throws IOException 将从off开始的指定字节数组中的len个字节写入基础流。
2 Public final int write(byte [] b)throws IOException 写入此数据输出流的当前字节数,返回写入缓冲区的总字节数。
3 public final void writeBooolean()throws IOException,public final void writeByte()throws IOException,public final void writeShort()throws IOException,public final void writeInt()throws IOException 这些方法将特定的原始类型数据作为字节写入输出流。
4 public void flush()throws IOException 刷新数据输出流。
5 public final void writeBytes(String s) throws IOException 将字符串作为字节序列写入基础输出流。通过丢弃其高8位,按顺序写入字符串中的每个字符。

示例

以下是演示如何使用DataInputStreamDataOutputStream的示例。 此示例读取文件test.txt中给出的5行,并将这些行转换为大写字母,最后将它们写入到另一个文件test1.txt中。

------ ----------
------ ----- ---------------- -

   ------ ------ ---- ----------- ------------- ----------- -

      -- --------- ----- ---
      ---------------- ------- - --- -------------------- ----------------------------------
      --------------------------

      -- ----------
      --------------- ------ - --- ------------------- ---------------------------------

      ------------------------------ -
         ------ - - -----------------
         -------------------- ---
      -
   -
-

执行上面示例代码,得到以下结果 -

---- -- ---- -  -
---- -- ---- -  -
---- -- ---- -  -
---- -- ---- -  -
---- -- ---- -  -

上一篇:Java方法
下一篇:Java异常处理