In questo tutorial impareremo a conoscere Java ByteArrayOutputStream e i suoi metodi con l'aiuto di esempi.
La ByteArrayOutputStream
classe del java.io
pacchetto può essere utilizzata per scrivere un array di dati di output (in byte).
Estende la OutputStream
classe astratta.
Nota : in ByteArrayOutputStream
mantiene una matrice interna di byte per memorizzare i dati.
Crea un ByteArrayOutputStream
Per creare un flusso di output di un array di byte, dobbiamo java.io.ByteArrayOutputStream
prima importare il pacchetto. Una volta importato il pacchetto, ecco come creare un flusso di output.
// Creates a ByteArrayOutputStream with default size ByteArrayOutputStream out = new ByteArrayOutputStream();
Qui abbiamo creato un flusso di output che scriverà i dati in un array di byte con dimensione predefinita 32 byte. Tuttavia, possiamo modificare la dimensione predefinita dell'array.
// Creating a ByteArrayOutputStream with specified size ByteArrayOutputStream out = new ByteArrayOutputStream(int size);
Qui, la dimensione specifica la lunghezza dell'array.
Metodi di ByteArrayOutputStream
La ByteArrayOutputStream
classe fornisce l'implementazione dei diversi metodi presenti nella OutputStream
classe.
metodo write ()
write(int byte)
- scrive il byte specificato nel flusso di outputwrite(byte() array)
- scrive i byte dall'array specificato nel flusso di outputwrite(byte() arr, int start, int length)
- scrive il numero di byte pari alla lunghezza nel flusso di output da un array a partire dalla posizione inizialewriteTo(ByteArrayOutputStream out1)
- scrive tutti i dati del flusso di output corrente nel flusso di output specificato
Esempio: ByteArrayOutputStream per scrivere dati
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(); ) ) )
Produzione
Flusso di output: questa è una riga di testo all'interno della stringa.
Nell'esempio precedente, abbiamo creato un flusso di output di array di byte denominato output.
ByteArrayOutputStream output = new ByteArrayOutputStream();
Per scrivere i dati nel flusso di output, abbiamo utilizzato il write()
metodo.
Nota : il getBytes()
metodo utilizzato nel programma converte una stringa in un array di byte.
Accedi ai dati da ByteArrayOutputStream
toByteArray()
- restituisce l'array presente all'interno del flusso di outputtoString()
- restituisce tutti i dati del flusso di output sotto forma di stringa
Per esempio,
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
Metodi | Descrizioni |
---|---|
size() | restituisce la dimensione della matrice nel flusso di output |
flush() | cancella il flusso di output |
To learn more, visit Java ByteArrayOutputStream (official Java documentation).