Java ByteArrayOutputStream (med eksempler)

I denne vejledning lærer vi om Java ByteArrayOutputStream og dens metoder ved hjælp af eksempler.

Den ByteArrayOutputStreamklasse af java.ioemballagen kan bruges til at skrive et array af uddata (i bytes).

Det udvider den OutputStreamabstrakte klasse.

Bemærk : In ByteArrayOutputStreamopretholder et internt array af bytes til lagring af dataene.

Opret en ByteArrayOutputStream

For at oprette en byte-array-outputstrøm skal vi først importere java.io.ByteArrayOutputStreampakken. Når vi først har importeret pakken, kan du oprette en outputstrøm her.

 // Creates a ByteArrayOutputStream with default size ByteArrayOutputStream out = new ByteArrayOutputStream(); 

Her har vi oprettet en outputstrøm, der skriver data til en række bytes med standardstørrelse 32 bytes. Vi kan dog ændre arrayets standardstørrelse.

 // Creating a ByteArrayOutputStream with specified size ByteArrayOutputStream out = new ByteArrayOutputStream(int size); 

Her angiver størrelsen længden på arrayet.

Metoder til ByteArrayOutputStream

Den ByteArrayOutputStreamklasse giver gennemførelsen af de forskellige metoder til stede i OutputStreamklassen.

skriv () metode

  • write(int byte) - skriver den specificerede byte til outputstrømmen
  • write(byte() array) - skriver bytes fra det specificerede array til output stream
  • write(byte() arr, int start, int length) - skriver antallet af byte svarende til længden til outputstrømmen fra en matrix startende fra positionens start
  • writeTo(ByteArrayOutputStream out1) - skriver hele data for den aktuelle outputstrøm til den specificerede outputstrøm

Eksempel: ByteArrayOutputStream til at skrive data

 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(); ) ) ) 

Produktion

 Output stream: Dette er en tekstlinje inde i strengen. 

I ovenstående eksempel har vi oprettet en byte-array-outputstrøm med navnet output.

 ByteArrayOutputStream output = new ByteArrayOutputStream(); 

For at skrive dataene til outputstrømmen har vi brugt write()metoden.

Bemærk : Den getBytes()metode, der bruges i programmet, konverterer en streng til en række bytes.

Få adgang til data fra ByteArrayOutputStream

  • toByteArray() - returnerer det array, der er til stede i outputstrømmen
  • toString() - returnerer hele dataene for outputstrømmen i strengform

For eksempel,

 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

Metoder Beskrivelser
size() returnerer størrelsen på arrayet i outputstrømmen
flush() rydder outputstrømmen

To learn more, visit Java ByteArrayOutputStream (official Java documentation).

Interessante artikler...