Java Tutorial/File/DataOutputStream

Материал из Java эксперт
Перейти к: навигация, поиск

Create DataOutputStream from BufferedOutputStream

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainClass {
  public static void main(String[] args) throws IOException {
    DataOutputStream out2 = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(
        "Data.txt")));
    out2.writeDouble(3.14159);
    out2.writeUTF("Square root of 2");
    out2.close();
    DataInputStream in5 = new DataInputStream(new BufferedInputStream(new FileInputStream(
        "Data.txt")));
    System.out.println(in5.readDouble());
    System.out.println(in5.readUTF());
  }
}





Create DataOutputStream from ByteArrayOutputStream

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainClass {
  public static void main(String args[]) throws IOException {
    int howMany = 20;
    // To avoid resizing the buffer, calculate the size of the
    // byte array in advance.
    ByteArrayOutputStream bout = new ByteArrayOutputStream(howMany * 4);
    DataOutputStream dout = new DataOutputStream(bout);
    for (int i = 0; i <= 20; i++) {
      dout.writeInt(i);
    }
    FileOutputStream fout = new FileOutputStream("fibonacci.dat");
    try {
      bout.writeTo(fout);
      fout.flush();
    } finally {
      fout.close();
    }
  }
}





DataOutputStream and DataInputStream: write and read primitive data type to a file

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Main {
  public static void main(String[] args) throws Exception{
    int idA = 1;
    String nameA = "City";
    int populationA = 5;
    float tempA = 1.0f;
    int idB = 2;
    String nameB = "S";
    int populationB = 2;
    float tempB = 1.45f;
    FileOutputStream fos = new FileOutputStream("cities.dat");
    DataOutputStream dos = new DataOutputStream(fos);
    dos.writeInt(idA);
    dos.writeUTF(nameA);
    dos.writeInt(populationA);
    dos.writeFloat(tempA);
    dos.writeInt(idB);
    dos.writeUTF(nameB);
    dos.writeInt(populationB);
    dos.writeFloat(tempB);
    dos.flush();
    dos.close();
    FileInputStream fis = new FileInputStream("cities.dat");
    DataInputStream dis = new DataInputStream(fis);
    int cityId = dis.readInt();
    System.out.println("City Id: " + cityId);
    String cityName = dis.readUTF();
    System.out.println("City Name: " + cityName);
    int cityPopulation = dis.readInt();
    System.out.println("City Population: " + cityPopulation);
    float cityTemperature = dis.readFloat();
    System.out.println("City Temperature: " + cityTemperature);
  }
}





Determine number of bytes written to DataOutputStream

import java.io.DataOutputStream;
import java.io.FileOutputStream;
public class Main {
  public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream("C:/Bytes.txt");
    DataOutputStream dos = new DataOutputStream(fos);
    dos.writeBytes("this is a test");
    int bytesWritten = dos.size();
    System.out.println("Total " + bytesWritten + " bytes are written to stream.");
    dos.close();
  }
}





Flush output stream

import java.io.DataOutputStream;
import java.io.FileOutputStream;
public class Main {
  public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream("C:/WriteByte.txt");
    DataOutputStream dos = new DataOutputStream(fos);
    dos.writeBytes("this is a test");
    dos.flush();
    dos.close();
  }
}





Write boolean to a file using DataOutputStream

import java.io.DataOutputStream;
import java.io.FileOutputStream;
public class Main {
  public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream("C:/WriteBoolean.txt");
    DataOutputStream dos = new DataOutputStream(fos);
    boolean b = false;
    dos.writeBoolean(b);
    dos.close();
  }
}





Write byte to a file using DataOutputStream

import java.io.DataOutputStream;
import java.io.FileOutputStream;
public class Main {
  public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream("C:/WriteByte.txt");
    DataOutputStream dos = new DataOutputStream(fos);
    int i = 1;
    dos.writeByte(i);
    dos.close();
  }
}





Write char to a file using DataOutputStream

import java.io.DataOutputStream;
import java.io.FileOutputStream;
public class Main {
  public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream("C:/WriteChar.txt");
    DataOutputStream dos = new DataOutputStream(fos);
    int ch = 65;
    dos.writeChar(ch);
    dos.close();
  }
}





Write double and UTF string using DataOutputStream with BufferedOutputStream backended

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainClass {
  public static void main(String[] args) throws IOException {
    DataOutputStream out2 = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(
        "Data.txt")));
    out2.writeDouble(3.14159);
    out2.writeUTF("Square root of 2");
    out2.close();
    DataInputStream in5 = new DataInputStream(new BufferedInputStream(new FileInputStream(
        "Data.txt")));
    System.out.println(in5.readDouble());
    System.out.println(in5.readUTF());
  }
}





Write float to a file using DataOutputStream

import java.io.DataOutputStream;
import java.io.FileOutputStream;
public class Main {
  public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream("C:/WriteFloat.txt");
    DataOutputStream dos = new DataOutputStream(fos);
    float f = 5.314f;
    dos.writeFloat(f);
    dos.close();
  }
}





Write integers using DataOutputStream

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainClass {
  public static void main(String[] args) throws IOException {
    DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(
        new File("temp.tmp"))));
    for (int i = 0; i < 10; i++)
      dos.writeInt(i);
    dos.close();
  }
}





Write int to a file using DataOutputStream

import java.io.DataOutputStream;
import java.io.FileOutputStream;
public class Main {
  public static void main(String[] args) throws Exception{
    FileOutputStream fos = new FileOutputStream("C:/Int.txt");
    DataOutputStream dos = new DataOutputStream(fos);
    int i = 100;
    dos.writeInt(i);
    dos.close();
  }
}





Write long to a file using DataOutputStream

import java.io.DataOutputStream;
import java.io.FileOutputStream;
public class Main {
  public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream("C:/WriteLong.txt");
    DataOutputStream dos = new DataOutputStream(fos);
    long l = 65;
    dos.writeLong(l);
    dos.close();
  }
}





Write short to a file using DataOutputStream

import java.io.DataOutputStream;
import java.io.FileOutputStream;
public class Main {
  public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream("C:/WriteShort.txt");
    DataOutputStream dos = new DataOutputStream(fos);
    short s = 1;
    dos.writeShort(s);
    dos.close();
  }
}





Write String as bytes to a file using DataOutputStream

import java.io.DataOutputStream;
import java.io.FileOutputStream;
public class Main {
  public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream("C:/Bytes.txt");
    DataOutputStream dos = new DataOutputStream(fos);
    String str = "this is test";
    dos.writeBytes(str);
    dos.close();
  }
}





Write String as characters to a file using DataOutputStream

import java.io.DataOutputStream;
import java.io.FileOutputStream;
public class Main {
  public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream("C:/Chars.txt");
    DataOutputStream dos = new DataOutputStream(fos);
    String str = "this is a test";
    dos.writeChars(str);
    dos.close();
  }
}