Home  >  Article  >  Java  >  Detailed explanation and example code of java DataInputStream and DataOutputStream

Detailed explanation and example code of java DataInputStream and DataOutputStream

高洛峰
高洛峰Original
2017-02-03 17:25:251866browse

java DataInputStream和DataOutputStream详解

操作基本数据类型的流 

DataInputStream

DataOutputStream

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
  
public class IntegerDemo {
  public static void main(String[] args) throws IOException {
    myWrite();
  
    myReader();
  }
  
  private static void myWrite() throws IOException {
    // TODO Auto-generated method stub
    // 创建数据输出流对象
    FileOutputStream fos = new FileOutputStream("E:\\zikao\\file\\cs.txt");
    DataOutputStream dos = new DataOutputStream(fos);
  
    // 写数据
    dos.writeByte(10);
    dos.writeShort(100);
    dos.writeInt(1000);
    dos.writeLong(10000);
    dos.writeFloat(12.34F);
    dos.writeDouble(12.56);
    dos.writeChar('a');
    dos.writeBoolean(true);
  
    // 释放资源
    dos.close();
  }
  
  private static void myReader() throws IOException {
    // TODO Auto-generated method stub
    // 创建数据输入流对象
    FileInputStream fis = new FileInputStream("E:\\zikao\\file\\cs.txt");
    DataInputStream dis = new DataInputStream(fis);
  
    // 读数据
    byte b = dis.readByte();
    short s = dis.readShort();
    int i = dis.readInt();
    long l = dis.readLong();
    float f = dis.readFloat();
    double d = dis.readDouble();
    char c = dis.readChar();
    boolean bl = dis.readBoolean();
  
    // 释放资源
    dis.close();
  
    System.out.println(b);
    System.out.println(s);
    System.out.println(i);
    System.out.println(l);
    System.out.println(f);
    System.out.println(d);
    System.out.println(c);
    System.out.println(bl);
  }
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多java DataInputStream和DataOutputStream详解及实例代码相关文章请关注PHP中文网!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn