English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Grund教程

Java Prozesssteuerung

Java Array

Java objektorientiert(I)

Java objektorientiert(II)

Java objektorientiert(III)

Java Ausnahmebehandlung

Java Liste(List)

Java Queue(Warteschlange)

Java Map-Kollektion

Java Set-Kollektion

Java Eingabe-Ausgabe(I/O)

Java Reader/Writer

Other Java Topics

Java OutputStream Klasse

In this tutorial, we will learn about Java OutputStream and its methods through an example.

The OutputStream class in the java.io package is an abstract superclass that represents a byte output stream.

Since OutputStream is an abstract class, it is not useful by itself. However, its subclasses can be used to write data.

OutputStream subclasses

To use the features of OutputStream, we can use its subclasses. Some of them are:

In the next tutorial, we will learn all these subclasses.

Create an OutputStream

To create an OutputStream, we must first import the java.io.OutputStream package. After importing the package, we can create an output stream.

//Create an OutputStream
OutputStream object = new FileOutputStream();

Here, we have created an output stream object FileOutputStream. Since OutputStream is an abstract class, we cannot create an object of OutputStream.

Note: We can also create output streams from other subclasses of OutputStream class.

OutputStream methods

The OutputStream class provides various methods implemented by its subclasses. Here are some of them:

  • write() - Write the specified bytes to the output stream

  • write(byte[] array) - Write the bytes in the specified array to the output stream

  • flush() -  Force all existing data in the output stream to be written to the target

  • close() - Schließen Sie den Ausgabestrom

Example: Using OutputStream of FileOutputStream

Below is the method implemented by the FileOutputStream class using OutputStream.

import java.io.FileOutputStream;
import java.io.OutputStream;
public class Main {
    public static void main(String args[]) {
        String data = "This is a line of text within the file.";
        try {
            OutputStream out = new FileOutputStream("output.txt");
            //Convert a string to bytes
            byte[] dataBytes = data.getBytes();
            //Daten in den Ausgabestrom schreiben
            out.write(dataBytes);
            System.out.println("Daten wurden in die Datei geschrieben.");
            //Schließen Sie den Ausgabestrom
            out.close();
        }
        catch (Exception e) {
            e.getStackTrace();
        }
    }
}

In dem obigen Beispiel haben wir mit der Klasse FileOutputStream eine Ausgabestrom erstellt. Jetzt ist der Ausgabestrom mit der Datei verbunden.output.txtVerknüpfung.

OutputStream out = new FileOutputStream("output.txt");

Um Daten in zu schreibenoutput.txtDatei, wir haben diese Methoden implementiert.

output.write();      //Daten in die Datei schreiben
output.close();      //Schließen Sie den Ausgabestrom

Wenn wir das Programm ausführen:output.txtDer Datei wird der folgende Inhalt hinzugefügt.

Dies ist ein Zeile Text im Dokument.