IO demo: DataOutputStream and DataInputStream : Java examples (example source code) » File Input Output » Data Input Output

Java
C++
Java Products
Java Articles
Java Home  »   File Input Output   » [  Data Input Output  ]  Screenshots 
 



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[] argsthrows IOException {
    DataOutputStream out = new DataOutputStream(new FileOutputStream(
        "Java2s.txt"));

    double[] prices = 19.999.9915.993.994.99 };
    int[] units = 128132950 };
    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.printlnunit );
        System.out.printlndesc );
        System.out.printlndesc );
        total = total + unit * price;
      }
    catch (EOFException e) {
    }
    in.close();
  }
}
Related examples in the same category
1.  Data IO Test 2 Data IO Test 2
2.  Data IO Demo Data IO Demo
3.  Data IO Test Data IO Test
4.  Typical I/O stream configurations
5.  ProgressMonitorInputStream Demo
6.  Some simple file I-O primitives reimplemented in Java
7.  ScanStreamTok - show scanning a file with StringTokenizer
8.  Write some data in binary
9.  Using transferTo() between channels
10.  Controlling serialization by adding your own writeObject() and readObject() methods Controlling serialization by adding your own writeObject() and readObject() methods
11.  Read Write Lock Test Read Write Lock Test








Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.