Die Anleitung zu Java IO Binary Streams

View more categories:

1- Die Überblick des Binäry Stream (Binary Stream)

Binär Stream wird durch 2 class InputStream und  OutputStream geleitet. Es gibt viele untergeordnete Class von dieser 2 class. lm Vegleich nach der Kraft ist die Binär Stream (Binary Stream) mehr und mehr kompliziert als die Charactor Stream. Mit JDK1.5 werden 2 Class LineNumberInputStream und  StringBufferInputStream in Binary Stream nicht empfohlen. Denn sie sind sehr veraltet

2- Class InputStream & OutputStream

Class InputStream ist ein Abstraktclass deshalb können Sie kein Objekt InputStream durch die class InputStream erstellen. Aber diese Class trennt in viele Zweige durch ihre untergeordneten Inheritance Class. Sie können die Objekt  InputStream aus den Komponenten von der untergeordneten Class in einigen Fällen erstellen
// Java.io.InputStream is an abstract class
// Could not initialize InputStream object directly, through class InputStream.
// Initialize InputStream object should be through its subclasses ..

InputStream fileStream =new FileInputStream("C:/test.txt");

// Input stream from the keyboard ..
InputStream is = System.in;
Class OutputStream ist ein Abstraktclass deshalb können Sie kein Objekt OutputStream durch die class OutputStream erstellen. Aber diese Class trennt in viele Zweige durch ihre untergeordneten Inheritance Class. Und das Wichtigkeit ist : Sie können die Objekt  InputStream aus den Komponenten von der untergeordneten Class in einigen Fällen erstellen
// Java.io.OutputStream is an abstract class
// Could not initialize OutputStream object directly, through class OutputStream.
// Initialize OutputStream object should be through its subclasses ..

// Stream write to file.
OutputStream os=new FileOutputStream("D:/outData.txt");

// Stream write to console.
OutputStream w=System.out;
HelloInputStream.java
package org.o7planning.tutorial.javaio.stream;

import java.io.FileInputStream;
import java.io.InputStream;

public class HelloInputStream {

    public static void main(String[] args) {
        try {
       
            // Create InputStream object from subclass.
            // This is Stream read file.
            InputStream is = new FileInputStream("data.txt");

            int i = -1;
           
            // Read the turn of bytes in the stream.
            // Each time the 8-bit read, convert it to int.
            // Read the value of -1 means the end of the stream.
            while ((i = is.read()) != -1) {
                System.out.println(i + "  " + (char) i);
            }
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
Das Ergebnis
HelloOutputStream.java
package org.o7planning.tutorial.javaio.stream;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class HelloOutputStream {

   public static void main(String[] args) {
       try {
           File dir = new File("C:/Test");

           // Create directories if not exists
           dir.mkdirs();

           // Create output Stream write data to file.
           OutputStream w = new FileOutputStream(
                   "C:/Test/test_outputStream.txt");
           
           // Create array of bytes, write it to stream.
           byte[] by = new byte[] { 'H', 'e', 'l', 'l', 'o' };

           // write turn the bytes into the stream
           for (int i = 0; i < by.length; i++) {
               byte b = by[i];

               // Write 1 byte.
               w.write(b);
           }
           // Close the output stream, finish write file.
           w.close();
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
}
Das Ergebnis
Die 2 obengenannten Beispiels sind einfach. Sie lesen oder schreiben jede Byte. Die nächsten Beispiele werden gleichzeigtig viele Byte lesen oder schreiben. Das hilft bei der Erhöhung der Verarbeitung
InputStreamExample2.java
package org.o7planning.tutorial.javaio.stream;

import java.io.FileInputStream;
import java.io.InputStream;

public class InputStreamExample2 {

    public static void main(String[] args) {
        try {

            // Create input stream, read a file.
            InputStream in = new FileInputStream("data.txt");

            // A temporary array to store data each reading
            byte[] temp = new byte[10];
            int i = -1;

           
            // Reads some number of bytes from the input stream
            // and stores them into the buffer array 'temp'.
            // Return the number of bytes actually read.
            // return -1 if end of stream.
            while ((i = in.read(temp)) != -1) {

                // Create String from bytes
                String s = new String(temp, 0, i);
                System.out.println(s);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Das ist die Image von dem gleichzeitigen Lesen von dem oben Beispiel
OutputStreamExample2.java
package org.o7planning.tutorial.javaio.stream;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class OutputStreamExample2 {

    public static void main(String[] args) {
        try {
            File dir = new File("C:/Test");

            // Create directories if not exists.
            dir.mkdirs();            
            
            // Create output Stream to write file.
            OutputStream os = new FileOutputStream("C:/Test/test_writerOutputStream.txt");
            
            // Create array of bytes, write bytes into the file above.
            byte[] by = new byte[] { 'H', 'e', 'l', 'l', 'o', ' ', 31, 34, 92 };
            byte[] by2 = new byte[] { 'H', 'e', 'l', 'l', 'o', ' ', 'b', 'o',
                    'y' };
            
            // Write all of bytes in array into Stream.
            os.write(by);
            
            // Flush data in memory to file.
            os.flush();
            
            // Continue write the 2nd byte array to the stream
            os.write(by2);
            
            // Close the output stream, finish write file.
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3- Class ByteArrayInputStream & ByteArrayOutputStream

ByteArrayInputStream hüllen ein Array von byte (byte[] buf) ein und durch ByteArrayInputStream greifen Sie die Arrayelementen
ByteArrayOutputStream ist einen Stream von byte, innerhalb des Objekt gibt es ein Array von byte (byte[] buf), die bei der steigernden Anzahl von Byte des Stream ihre Größe selbst erhöhen kann .Jedes Mal wird die Stream die Byte geschrieben, wird diese Byte in die noch nicht zugewiesene Arrayposition zugewiesen  ..
  •  Wenn der Array voll mit der Elementen  sind, wird ein neuer Array mit einer größeren Länge erstellt und die Elemente in dem alten Array werden in dem neu erstellten Array kopiert... (Das ist die selbste Erhöhung des Character Array wie oben gesagt)
Einige Methode von ByteArrayOutputStream:
 
// return the current contents of this output stream, as a byte array.
  - byte[] toByteArray();

// String decoded from the buffer's contents.
  - String toString() ;

// return the number of valid bytes in this output stream.
  - int size();
ByteArrayInputStreamExample.java
package org.o7planning.tutorial.javaio.bytestream;

import java.io.ByteArrayInputStream;
import java.io.IOException;

public class ByteArrayInputStreamExample {

    public static void main(String args[]) throws IOException {

        // Byte Array.
        byte[] bytes = new byte[] { 'H', 'e', 'l', 'l', 'o', ' ', 'I', 'O' };

        // Using ByteArrayInputStream to read bytes array.
        ByteArrayInputStream bInput = new ByteArrayInputStream(bytes);

        System.out.println("Converting characters to Upper case ");
        int c = 0;

        // Read the turn of bytes in the stream.
        // Cursor will move from the beginning to the end of the array array.
        // Every time you read a byte pointer will move one step to the end.        
        while ((c = bInput.read()) != -1) {
            char ch = (char) c;
            ch = Character.toUpperCase(ch);
            System.out.println(ch);
        }

        // Check whether this stream supports mark or not
        boolean markSupport = bInput.markSupported();

        System.out.println("Mark Support? " + markSupport);
       
        // Move the cursor to the default location
        // In this example, it will move to position 0 ..        
        bInput.reset();

        char ch = (char) bInput.read();
        System.out.println(ch);

         // Read next byte
        ch = (char) bInput.read();
        System.out.println(ch);

        System.out.println("Skip 4");
 
        // Skip 4 bytes
        bInput.skip(4);
        ch = (char) bInput.read();
        System.out.println(ch);

    }

}
Das Ergebnis
ByteArrayOutputStreamExample.java
package org.o7planning.tutorial.javaio.bytestream;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class ByteArrayOutputStreamExample {

    public static void main(String args[]) throws IOException {


        // Create ByteArrayOutputStream object.
        // Object contains within it an array of bytes.
        // Array with size 12 elements.
        // If the number of elements to write to stream more than 12, the array will be replaced by
        // new array has more elements, and copy the elements of old array into.        
        ByteArrayOutputStream bOutput = new ByteArrayOutputStream(12);

        String s = "Hello ByteArrayOutputStream";

        for (int i = 0; i < s.length(); i++) {

            char ch = s.charAt(i);

            if (ch != 'a' && ch != 'e') {
                bOutput.write(ch);
            }
        }

        // Returns the current size of the buffer.
        int size = bOutput.size();
        System.out.println("Size = " + size);

        byte[] bytes = bOutput.toByteArray();

        String ss = new String(bytes);

        System.out.println("New String = " + ss);
    }
}
Das Ergebnis

4- Class ObjectInputStream & ObjectOutputStream

ObjectInputStream, ObjectOutputStream ermöglichen Ihnen, ein Objekt in einem Stream zu schreiben oder lesen. Diese Object müssen die  Serializable Type sein .
Unten sind die einige Class für Beispiel
Student.java
package org.o7planning.tutorial.javaio.objstream;

import java.io.Serializable;

public class Student implements Serializable {

   private static final long serialVersionUID = -5074534753977873204L;

   private String firstName;

   private String lastName;

   public Student(String firstName, String lastName) {
       this.firstName = firstName;
       this.lastName = lastName;
   }

   public String getFirstName() {
       return firstName;
   }

   public void setFirstName(String firstName) {
       this.firstName = firstName;
   }

   public String getLastName() {
       return lastName;
   }

   public void setLastName(String lastName) {
       this.lastName = lastName;
   }

}
Pupil.java
package org.o7planning.tutorial.javaio.objstream;

import java.io.Serializable;

public class Pupil implements Serializable {
 
   private static final long serialVersionUID = -8501383434011302991L;
   
   private String fullName;

   public Pupil(String fullName)  {
       this.fullName= fullName;
   }
   
   public String getFullName() {
       return fullName;
   }

   public void setFullName(String fullName) {
       this.fullName = fullName;
   }

}
ObjectOutputStreamExample.java
package org.o7planning.tutorial.javaio.objstream;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Date;

public class ObjectOutputStreamExample {

    public static void main(String[] args) throws IOException {
        File dir = new File("C:/Test");

        // Create directories if not exists.
        dir.mkdirs();


        // Create stream write to file.
        FileOutputStream fos = new FileOutputStream(
                "C:/Test/testObjectStream.txt");

       
        // Create ObjectOutputStream object wrap 'fos'.
        // Data written to this stream will be pushed to 'fos'.
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        
        
        // Write String to Stream.
        oos.writeUTF("This is student, pupil profiles");
    
  
        // Note: Write Serializable object only.
        // Write an Object to stream.
        oos.writeObject(new Date());

        Student student1 = new Student("Thanh", "Phan");
        Student student2 = new Student("Ngan", "Tran");
        Pupil pupil1 = new Pupil("Nguyen Van Ba");

        oos.writeObject(student1);
        oos.writeObject(pupil1);
        oos.writeObject(student2);

        oos.close();
        System.out.println("Write successful");
    }

}
Das Ergebnis
Das ist die Image des Schreibens des Object in file. Das Schreiben nach einer Reihenfolge. Später wenn Sie lesen, lesen Sie nach der Reihenfolge von dem vorherigen Schreiben
Und Beispiel mit ObjectInputStream für das Lesen der im oben Beispiel geschriebenen file 
ObjectInputStreamExample.java
package org.o7planning.tutorial.javaio.objstream;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Date;

public class ObjectInputStreamExample {

    public static void main(String[] args) throws IOException,
            ClassNotFoundException {

        // Create stream to read file.
        FileInputStream fis = new FileInputStream(
                "C:/Test/testObjectStream.txt");

        // Create ObjectInputStream object wrap 'fis'.
        ObjectInputStream ois = new ObjectInputStream(fis);

        // Read String.
        String s = ois.readUTF();

        System.out.println(s);

        // Read Object.
        Date date = (Date) ois.readObject();
        System.out.println("Date = " + date);

        Student student1 = (Student) ois.readObject();

        System.out.println("Student " + student1.getFirstName());

        Pupil pupil = (Pupil) ois.readObject();

        System.out.println("Pupil " + pupil.getFullName());

        Student student2 = (Student) ois.readObject();

        System.out.println("Student " + student2.getFirstName());

        ois.close();
    }
}
Ergebnis

5- Class DataInputStream & DataOutputStream

DataOutputStream **
// Constructor
public DataOutputStream(OutputStream out)

// Write a character 16 bit (2-byte)
public void writeChar(int val)
// Write double 64 bit (8-byte)
public void writeDouble(double val)
// Write float 32 bit (4-byte)
public void writeFloat(float val)
// Write integer 32 bit (4-byte)
public void writeInt(int val)
// Write String UTF-8.
public void writeUTF(String obj)
....
DataInputStream **
// Constructor
public DataInputStream(InputStream in)

// Read a character 16 bit (2 byte)
public char readChar()
// Read double 64 bit (8 byte)
public double readDouble()
// Read float 32 bit (4 byte)
public float readFloat()
// Read int 16 bit (4 byte)
public int readInt()
// Read UTF-8 String.
public String readUTF()
....
DataOutputStreamExample.java
package org.o7planning.tutorial.javaio.datastream;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataOutputStreamExample {

    public static void main(String[] args) throws IOException {
        int cityIdA = 1;
        String cityNameA = "Green Lake City";
        int cityPopulationA = 500000;
        float cityTempA = 15.50f;

        int cityIdB = 2;
        String cityNameB = "Salt Lake City";
        int cityPopulationB = 250000;
        float cityTempB = 10.45f;

        File dir = new File("C:/Test");
        dir.mkdirs();

        //
        // Create FileOutputStream write to file.
        //
        FileOutputStream fos = new FileOutputStream("C:/Test/cities.txt");

        // Create DataOutputStream object wrap 'fos'.
        // The data write to 'dos' will be pushed to 'fos'.
        DataOutputStream dos = new DataOutputStream(fos);

        //
        // Write data.
        //
        dos.writeInt(cityIdA);
        dos.writeUTF(cityNameA);
        dos.writeInt(cityPopulationA);
        dos.writeFloat(cityTempA);

        dos.writeInt(cityIdB);
        dos.writeUTF(cityNameB);
        dos.writeInt(cityPopulationB);
        dos.writeFloat(cityTempB);

        dos.flush();
        dos.close();
    }

}
Starten Sie class DataOutputStreamExample und erhalten Sie eine geschriebenen Daten File
DataInputStreamExample.java
package org.o7planning.tutorial.javaio.datastream;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class DataInputStreamExample {
    
    
    
    public static void main(String[] args) throws IOException {
        
         // Stream to read file.
         FileInputStream fis = new FileInputStream("C:/Test/cities.txt");

         // Create DataInputStream object wrap 'fis'.
         DataInputStream dis = new DataInputStream(fis);

         //
         // Read data.
         //
         int cityId1 = dis.readInt();
         System.out.println("Id: " + cityId1);
         String cityName1 = dis.readUTF();
         System.out.println("Name: " + cityName1);
         int cityPopulation1 = dis.readInt();
         System.out.println("Population: " + cityPopulation1);
         float cityTemperature1 = dis.readFloat();
         System.out.println("Temperature: " + cityTemperature1);

         //
         // Read data.
         //
         int cityId2 = dis.readInt();
         System.out.println("Id: " + cityId2);
         String cityName2 = dis.readUTF();
         System.out.println("Name: " + cityName2);
         int cityPopulation2 = dis.readInt();
         System.out.println("Population: " + cityPopulation2);
         float cityTemperature2 = dis.readFloat();
         System.out.println("Temperature: " + cityTemperature2);
         
         dis.close();
    }
}
Das Ergebnis

6- SequenceInputStream

Normalerweise sind Sie gewöhnlich, ein File zu lesen und ein Input Stream zu erhalten. Aber in der Praxis lesen Sie viele File und legen die Daten zusammen um eine File zu schreiben. D.h die Idee von der Verbindung vieler Input Stream zu der Erstellung einer hintereinander größeren Stream. Wir erwähnen die class  java.io.SequenceInputStream. Dieser Begriff entspricht keine Outputstream ...
// Constructor

 // Create new Stream from Pairing two streams together
// which will be read in order
public SequenceInputStream(InputStream s1,InputStream s2)  
 
// Create new Stream from Multi input stream.
// which will be read in order
  public SequenceInputStream(Enumeration<? extends InputStream> e)
Beispiel
// Input stream read a file - File1.txt .
InputStream is1=new FileInputStream("File1.txt");
// Input stram read a file - File2.txt  
InputStream is2=new FileInputStream("File2.txt");

// Create new Stream from two stream
SequenceInputStream sis=new SequenceInputStream(is1,is2);

7- PipedInputStream & PipedOutputStream

Angenommen, Sie haben 2 Streams: Input und Output. Das Inputstream von A Daten liest eine File und nimmt die Information vom Stream zum Aufschreiben auf B Daten Stream, dessen Output eine andere File ist. Die Streams A und B sind in diesem Fall getrennt... Deshalb brauchen Sie 3 Aktions in der Applikation 
  1. Erstellung der Daten Stream Lesen  A
  2. Erstellung der Daten Stream Schreiben B
  3. Lesen von A  Schreiben in B 
Die 2 Aktions sind zwanghaft aber Sie möchten die 3.Aktion entwerfen  ... d.h es gibt eine implizite Beziehung zwischen 2 InputStream und OutputStream, damit wenn die Byte,die auf dem A LesenStream auftritt, auf B Stream automatische aufgeschrieben werden ... Das ist die implizite Beziehung zwischen Inputstream und Outputstream sogenannt.
PipeStreamExample1.java
package org.o7planning.tutorial.javaio.pipestream;

import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class PipeStreamExample1 {

	private InputStream pipedInputStream;

	public static void main(String[] args) throws IOException, InterruptedException {
		new PipeStreamExample1().test();
	}

	private void test() throws IOException, InterruptedException {
		// Create a 'pipedOutputStream',
		PipedOutputStream pipedOutputStream = new PipedOutputStream();

		// Data writing to 'pipedOutputStream'
		// will automatically appear in 'pipedInputStream'.
		pipedInputStream = new PipedInputStream(pipedOutputStream);

		new ThreadRead().start();

		char[] chs = new char[] { 'a', 'a', 'b', 'c' , 'e' };

		// Write data to 'pipedOutputStream'.
		for (char ch : chs) {
			pipedOutputStream.write(ch);
			Thread.sleep(1000);
		}
		pipedOutputStream.close();
	}

	// A Thread to read the data that appears on 'pipedInputStream'.
	class ThreadRead extends Thread {

		@Override
		public void run() {
			try {
				int data = 0;
				while ((data = pipedInputStream.read()) != -1) {
					System.out.println((char) data);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				closeQuietly(pipedInputStream);
			}
		}
	}

	private void closeQuietly(InputStream is) {
		if (is != null) {
			try {
				is.close();
			} catch (IOException e) {
			}
		}
	}

}

8- Class PrintStream


// PrintStream is the subclass of FilterOutputStream.
// It can wrap a binary output stream (OutputStream) , ..
// Constructor :

   // Wrap a OutputStream
   public PrintStream(OutputStream out)

   public PrintStream(OutputStream out,boolean autoFlush)

   // Write to file ..
   public PrintStream(String fileName)
 

// Some methods
   public void println(String s)
   public void print(char ch)

   // Write an Object
   public void print(Object obj)

   // Write long value (64bit)
   public void print(long n)

   public PrintStream append(java.lang.CharSequence csq) .
   // ... (more see javadoc)
Sie gewöhnen daran, die Ausnahme Exception durch die Block  try, catch. zu greiffen
try {    
 // Do something here
 // Error divided by 0
 int i=10/0;
}
// The catch block is executed
catch(Exception e) {  
 // Print out message
 System.out.println("Error on try..."+e.getMessage());
 // Print 'stack trace' to Console.
 // How to get the text "stack trace"?
 e.printStackTrace();
}
Das ist 'Stack Trace' (beim Error oft erscheint)
Das unten Beispiel nimmt "Stack Trace" raus
GetStackTraceString.java
package org.o7planning.tutorial.javaio.printstream;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class GetStackTraceString {

  private static String getStackTraceString(Exception e) {
 
      // Create ByteArrayOutputStream
      ByteArrayOutputStream baos = new ByteArrayOutputStream();


      // Data written to this stream will be pushed to 'baos'.
      PrintStream printStream = new PrintStream(baos);


      // Prints this throwable and its backtrace to 'printStream'
      e.printStackTrace(printStream);

      printStream.close();
     
      byte[] bytes = baos.toByteArray();

      String s = new String(bytes);
      return s;
  }

  public static void main(String[] args) {

      try {
          // Do something here
          // Error divided by 0
          int i = 10 / 0;
      }
      // The catch block is executed
      catch (Exception e) {
          // Print out message
          System.out.println("Error on try..." + e.getMessage());

          // Get the text "stack trace"
          String s = getStackTraceString(e);

          System.out.println("Stack Trace String " + s);
      }
  }
}
Die folgende Code schreiben die Information Debug in Bildchirm Console aber es benutzt die Method printStackTrace(PrintStream ps) von der Exception statt dem Method printStackTrace() , die standardmäßig für das Zweck von dem Debug ins Console tätig ist
// PrintStream to write to the Console
PrintStream os = System.out;
// Exception e ..
// Write 'stack trace' to 'os'. This means print to the Console.
e.printStackTrace(os);

// In essence it is equivalent to calling:
e.printStackTrace();

View more categories: