目录

DataOutputStream

DataOutputStream流允许您将基元写入输出源。

以下是创建DataOutputStream的构造函数。

DataOutputStream out = DataOutputStream(OutputStream out);

一旦掌握了DataOutputStream对象,就会有一个辅助方法列表,可用于编写流或在流上执行其他操作。

Sr.No. 方法和描述
1

public final void write(byte[] w, int off, int len)throws IOException

将从关闭点开始的指定字节数组中的len个字节写入基础流。

2

Public final int write(byte [] b)throws IOException

写入写入此数据输出流的当前字节数。 返回写入缓冲区的总字节数。

3

(a) public final void writeBooolean()throws IOException,

(b) public final void writeByte()throws IOException,

(c) public final void writeShort()throws IOException

(d) public final void writeInt()throws IOException

这些方法将特定的基本类型数据作为字节写入输出流。

4

Public void flush()throws IOException

刷新数据输出流。

5

public final void writeBytes(String s) throws IOException

将字符串作为字节序列写入基础输出流。 通过丢弃其高8位,按顺序写出字符串中的每个字符。

例子 (Example)

以下是演示DataInputStream和DataOutputStream的示例。 此示例读取文件test.txt中给出的5行,并将这些行转换为大写字母,最后将它们复制到另一个文件test1.txt中。

import java.io.*;
public class DataInput_Stream {
   public static void main(String args[])throws IOException {
      // writing string to a file encoded as modified UTF-8
      DataOutputStream dataOut = new DataOutputStream(new FileOutputStream("E:\\file.txt"));
      dataOut.writeUTF("hello");
      // Reading data from the same file
      DataInputStream dataIn = new DataInputStream(new FileInputStream("E:\\file.txt"));
      while(dataIn.available()>0) {
         String k = dataIn.readUTF();
         System.out.print(k+" ");
      }
   }
}

以下是上述程序的示例运行 -

输出 (Output)

THIS IS TEST 1  ,
THIS IS TEST 2  ,
THIS IS TEST 3  ,
THIS IS TEST 4  ,
THIS IS TEST 5  ,
↑回到顶部↑
WIKI教程 @2018