Šajā apmācībā mēs ar piemēru palīdzību uzzināsim par Java ByteArrayOutputStream un tā metodēm.
ByteArrayOutputStream
No klases java.io
paketē var izmantot, lai rakstītu masīvu izejas datus (baitos).
Tas paplašina OutputStream
abstrakto klasi.
Piezīme : In ByteArrayOutputStream
saglabā iekšējo baitu masīvu datu glabāšanai.
Izveidojiet ByteArrayOutputStream
Lai izveidotu baitu masīva izvades straumi, mums vispirms jāimportē java.io.ByteArrayOutputStream
pakete. Kad pakotne ir importēta, mēs varam izveidot izvades straumi.
// Creates a ByteArrayOutputStream with default size ByteArrayOutputStream out = new ByteArrayOutputStream();
Šeit mēs esam izveidojuši izvades straumi, kas ierakstīs datus baitu masīvā ar noklusējuma 32 baitu lielumu. Tomēr mēs varam mainīt masīva noklusējuma lielumu.
// Creating a ByteArrayOutputStream with specified size ByteArrayOutputStream out = new ByteArrayOutputStream(int size);
Šeit lielums norāda masīva garumu.
ByteArrayOutputStream metodes
ByteArrayOutputStream
Klase paredz nodrošināt dažādu metožu, kas atrodas OutputStream
klasē.
write () metode
write(int byte)
- izejas straumē ieraksta norādīto baituwrite(byte() array)
- raksta baitus no norādītā masīva uz izejas straumiwrite(byte() arr, int start, int length)
- raksta baitu skaitu, kas vienāds ar garumu izejas plūsmai no masīva, sākot no pozīcijas sākumawriteTo(ByteArrayOutputStream out1)
- visu pašreizējās izvades plūsmas datus ieraksta norādītajā izvades straumē
Piemērs: ByteArrayOutputStream, lai ierakstītu datus
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(); ) ) )
Rezultāts
Izejas straume: šī ir teksta rindiņa virknes iekšpusē.
Iepriekš minētajā piemērā mēs esam izveidojuši baitu masīva izvades plūsmu ar nosaukumu output.
ByteArrayOutputStream output = new ByteArrayOutputStream();
Lai ierakstītu datus izejas straumē, mēs izmantojām write()
metodi.
Piezīme : Programmā getBytes()
izmantotā metode pārveido virkni baitu masīvā.
Piekļūstiet datiem no ByteArrayOutputStream
toByteArray()
- atgriež masīvu, kas atrodas izvades straumētoString()
- atgriež visus izvades straumes datus virknes formā
Piemēram,
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
Metodes | Apraksti |
---|---|
size() | atgriež masīva izmēru izvades straumē |
flush() | notīra izvades straumi |
To learn more, visit Java ByteArrayOutputStream (official Java documentation).