import java.io.*;

    // Schreiben und lesen von integer und double
    // Schreiben und lesen eines String
    // Problem writeChars vs. readChars

  public class Readwrite_int_double_2 {

  private static void pause() {
    try {
      System.in.read();
    }
    catch (IOException e) {
    }
  }  // pause


  private void write1() {
     try {
          System.out.println("write1");
          FileOutputStream fout = new FileOutputStream("readwrite1.bin");  // byteweise
           		// Ausgabe nun mittels primitiver Datentypen
          DataOutputStream dout = new DataOutputStream(fout);
          int i = 12345;
          long ii=1234;
          dout.writeInt(i);  // 4 Bytes
          dout.writeLong(ii);  // 8 Bytes
          dout.writeChar('a'); 
          dout.writeChar('b'); 
          dout.writeChar('c'); 
          // dout.writeChars(s); // ??
          String s = "abcdef";
          dout.writeUTF(s); 
          dout.writeDouble(123.4567); 
          dout.close();
      }
      catch (IOException e) {
           System.err.println("IOException: " + e);
      }
    }  // write1

  private void read1() {
     System.out.println("\n\nread1");
    try {
          FileInputStream fin = new FileInputStream("readwrite1.bin");  // byteweise
           		// Ausgabe nun mittels primitiver Datentypen
          DataInputStream din = new DataInputStream(fin);
          int i,n;
          long ii;
          char ch;
          double d;
          i = din.readInt();  // 4 Bytes
          System.out.println("i: "+i);
          ii=din.readLong();  // 8 Bytes
          System.out.println("long: "+ii);
          ch=din.readChar();   // a
          System.out.println("char ch: "+ch);
          ch=din.readChar(); 
          System.out.println("char ch: "+ch);
          ch=din.readChar(); 
          System.out.println("char ch: "+ch);
          String s;
          //din.readChars(); 
          s = din.readUTF(); 
          System.out.println("String s: "+s);
          d = din.readDouble(); 
          System.out.println("double d: "+d);
          din.close();
        }
        catch (IOException e) {
            System.err.println("IOException: " + e);
        }
  }  // read1


 private void write2() {
     System.out.println("\n\nwrite2");
    try {
          FileOutputStream fout = new FileOutputStream("readwrite1.bin");  // byteweise
           		// Ausgabe nun mittels primitiver Datentypen
          DataOutputStream dout = new DataOutputStream(fout);
          int i = 12345;
          long ii=1234;
          dout.writeInt(i);  // 4 Bytes
          dout.writeLong(ii);  // 8 Bytes
          dout.writeChar('a'); 
          dout.writeChar('b'); 
          dout.writeChar('c'); 
          String s = "abcdef";
          dout.writeInt(s.length());  // 4 Bytes
          dout.writeChars(s); 
          dout.writeUTF(s); 
          dout.writeDouble(123.4567); 
          dout.close();
     }
      catch (IOException e) {
          System.err.println("IOException: " + e);
      }
   }  // write2

  private void read2() {
     System.out.println("\n\nread2");
    try {
          FileInputStream fin = new FileInputStream("readwrite1.bin");  // byteweise
           		// Ausgabe nun mittels primitiver Datentypen
          DataInputStream din = new DataInputStream(fin);
          int i,n;
          long ii;
          char ch;
          double d;
          i = din.readInt();  // 4 Bytes
          System.out.println("i: "+i);
          ii=din.readLong();  // 8 Bytes
          System.out.println("long: "+ii);
          ch=din.readChar();   // a
          System.out.println("char ch: "+ch);
          ch=din.readChar(); 
          System.out.println("char ch: "+ch);
          ch=din.readChar(); 
          System.out.println("char ch: "+ch);
          String s="";
          n = din.readInt(); // Anzahl der Buchstaben
          for (int j=0; j<n; j++) {
             s=s+din.readChar(); 
          }
          System.out.println("readChars s: "+s);
          s = din.readUTF(); 
          System.out.println("String s: "+s);
          d = din.readDouble(); 
          System.out.println("double d: "+d);
          din.close();
        }
        catch (IOException e) {
            System.err.println("IOException: " + e);
        }
   }  // read2

  public static void main(String argv[]) {
      Readwrite_int_double_2  main = new Readwrite_int_double_2();
      main.write1();
      main.read1();
      main.write2();
      main.read2();
      pause();
  }  // main

}



