In deze tutorial leren we met behulp van voorbeelden over Java ByteArrayOutputStream en zijn methoden.
De ByteArrayOutputStream
klasse van het java.io
pakket kan worden gebruikt om een reeks uitvoergegevens (in bytes) te schrijven.
Het breidt de OutputStream
abstracte klasse uit.
Opmerking : In ByteArrayOutputStream
onderhoudt een interne reeks bytes om de gegevens op te slaan.
Maak een ByteArrayOutputStream
Om een byte-array-uitvoerstroom te maken, moeten we het java.io.ByteArrayOutputStream
pakket eerst importeren . Zodra we het pakket hebben geïmporteerd, kunnen we als volgt een uitvoerstroom maken.
// Creates a ByteArrayOutputStream with default size ByteArrayOutputStream out = new ByteArrayOutputStream();
Hier hebben we een uitvoerstroom gemaakt die gegevens naar een array van bytes met een standaardgrootte van 32 bytes zal schrijven. We kunnen echter de standaardgrootte van de array wijzigen.
// Creating a ByteArrayOutputStream with specified size ByteArrayOutputStream out = new ByteArrayOutputStream(int size);
Hier geeft de grootte de lengte van de array aan.
Methoden van ByteArrayOutputStream
De ByteArrayOutputStream
klasse zorgt voor de implementatie van de verschillende methoden die in de OutputStream
klas aanwezig zijn.
write () methode
write(int byte)
- schrijft de opgegeven byte naar de uitvoerstroomwrite(byte() array)
- schrijft de bytes van de opgegeven array naar de uitvoerstroomwrite(byte() arr, int start, int length)
- schrijft het aantal bytes dat gelijk is aan de lengte naar de uitvoerstroom vanuit een array vanaf het begin van de positiewriteTo(ByteArrayOutputStream out1)
- schrijft de volledige data van de huidige outputstroom naar de gespecificeerde outputstroom
Voorbeeld: ByteArrayOutputStream om gegevens te schrijven
import java.io.ByteArrayOutputStream; class Main ( public static void main(String() args) ( String data = "This is a line of text inside the string."; try ( // Creates an output stream ByteArrayOutputStream out = new ByteArrayOutputStream(); byte() array = data.getBytes(); // Writes data to the output stream out.write(array); // Retrieves data from the output stream in string format String streamData = out.toString(); System.out.println("Output stream: " + streamData); out.close(); ) catch(Exception e) ( e.getStackTrace(); ) ) )
Uitvoer
Uitvoerstroom: dit is een regel tekst binnen de string.
In het bovenstaande voorbeeld hebben we een byte-array-uitvoerstroom gemaakt met de naam output.
ByteArrayOutputStream output = new ByteArrayOutputStream();
Om de gegevens naar de outputstroom te schrijven, hebben we de write()
methode gebruikt.
Opmerking : de getBytes()
methode die in het programma wordt gebruikt, zet een string om in een array van bytes.
Toegang tot gegevens van ByteArrayOutputStream
toByteArray()
- geeft de array terug die aanwezig is in de uitvoerstroomtoString()
- retourneert de volledige gegevens van de uitvoerstroom in tekenreeksvorm
Bijvoorbeeld,
import java.io.ByteArrayOutputStream; class Main ( public static void main(String() args) ( String data = "This is data."; try ( // Creates an output stream ByteArrayOutputStream out = new ByteArrayOutputStream(); // Writes data to the output stream out.write(data.getBytes()); // Returns an array of bytes byte() byteData = out.toByteArray(); System.out.print("Data using toByteArray(): "); for(int i=0; i
Output
Data using toByteArray(): This is data. Data using toString(): This is data.
In the above example, we have created an array of bytes to store the data returned by the
toByteArray()
method.
We then have used the for loop to access each byte from the array. Here, each byte is converted into the corresponding character using typecasting.
close() Method
To close the output stream, we can use the
close()
method.
However, the
close()
method has no effect in ByteArrayOutputStream
class. We can use the methods of this class even after the close()
method is called.
Other Methods of ByteArrayOutputStream
Methoden | Beschrijvingen |
---|---|
size() | geeft de grootte van de array in de uitvoerstroom terug |
flush() | wist de outputstroom |
To learn more, visit Java ByteArrayOutputStream (official Java documentation).