IO demo: DataOutputStream and DataInputStream : Data Input Output « File Input Output « Java






IO demo: DataOutputStream and DataInputStream

 

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataIODemo1 {
  public static void main(String[] args) throws IOException {
    DataOutputStream out = new DataOutputStream(new FileOutputStream(
        "Java2s.txt"));

    double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };
    int[] units = { 12, 8, 13, 29, 50 };
    String[] descs = { "Java", "Source ", "and",
        "Support."};

    for (int i = 0; i < prices.length; i++) {
      out.writeDouble(prices[i]);
      out.writeChar('\t');
      out.writeInt(units[i]);
      out.writeChar('\t');
      out.writeChars(descs[i]);
      out.writeChar('\n');
    }
    out.close();

    // read it in again
    DataInputStream in = new DataInputStream(new FileInputStream(
        "Java2s.txt"));

    double price;
    int unit;
    String desc;
    double total = 0.0;

    try {
      while (true) {
        price = in.readDouble();
        in.readChar(); // throws out the tab
        unit = in.readInt();
        in.readChar(); // throws out the tab
        desc = in.readLine();
        System.out.println( unit );
        System.out.println( desc );
        System.out.println( desc );
        total = total + unit * price;
      }
    } catch (EOFException e) {
    }
    in.close();
  }
}

           
         
  








Related examples in the same category

1.The use of DataOutputStream and DataInputStream:
2.Data IO Test 2Data IO Test 2
3.Data IO DemoData IO Demo
4.Data IO Test Data IO Test
5.Typical I/O stream configurations
6.ProgressMonitorInputStream Demo
7.Some simple file I-O primitives reimplemented in Java
8.ScanStreamTok - show scanning a file with StringTokenizer
9.Write some data in binary
10.Using transferTo() between channels
11.Controlling serialization by adding your own writeObject() and readObject() methodsControlling serialization by adding your own writeObject() and readObject() methods
12.Read Write Lock TestRead Write Lock Test
13.Bit InputStream