Razred Java BufferedOutputStream
V Javi se razred BufferedOutputStream uporablja za učinkovitejše zapisovanje podatkov v izhodni tok. Drugemu izhodnemu toku doda medpomnilnik, s čimer zmanjša število V/I operacij z začasnim shranjevanjem podatkov v pomnilnik, preden jih zapiše na cilj (kot datoteko).
Spada v paket java.io in razširja razred FilterOutputStream.
Izjava razreda
javni razred BufferedOutputStream razširja FilterOutputStream
- Izboljša V/I zmogljivost z uporabo medpomnilnika za zmanjšanje neposrednega dostopa do diska.
- Podatki se zapišejo v vmesni pomnilnik in le, ko je vmesni pomnilnik poln (ali izpraznjen), se pošljejo osnovnemu izhodnemu toku.
- Običajno se uporablja z FileOutputStream.
- Pomaga pri zapisovanju bajtnih podatkov, kot so slikovne besedilne datoteke in binarni podatki.
Konstruktorji
- BufferedOutputStream(OutputStream out): Ustvari medpomnilnik izhodni tok s privzeto velikostjo medpomnilnika 8192 bajtov.
- BufferedOutputStream(OutputStream out int size): Ustvari medpomnilnik izhodni tok s podano velikostjo medpomnilnika.
Pomembne metode
| Metoda | Opis |
|---|---|
void write(int b) | Zapiše en bajt v izhodni tok. |
void write(byte[] b int off int len) | Zapiše del niza bajtov v izhodni tok. |
void flush() | Izprazni vmesni pomnilnik in vsili zapisovanje vseh vmesnih izhodnih bajtov. |
void close() | Zapre tok in ga najprej splakne. |
Java import java.io.* ; public class BufferedOutputStreamExample { public static void main ( String [] args ) { String data = 'BufferedOutputStream in Java Example' ; try { FileOutputStream fileOut = new FileOutputStream ( 'output.txt' ); BufferedOutputStream bufferOut = new BufferedOutputStream ( fileOut ); byte [] bytes = data . getBytes (); bufferOut . write ( bytes ); bufferOut . close (); System . out . println ( 'Data written successfully.' ); } catch ( IOException e ) { e . printStackTrace (); } } }
import java.io.* ; public class BufferedOutputStreamExample { public static void main ( String [] args ) { String data = 'BufferedOutputStream in Java Example' ; try { FileOutputStream fileOut = new FileOutputStream ( 'output.txt' ); BufferedOutputStream bufferOut = new BufferedOutputStream ( fileOut ); byte [] bytes = data . getBytes (); bufferOut . write ( bytes ); bufferOut . close (); System . out . println ( 'Data written successfully.' ); } catch ( IOException e ) { e . printStackTrace (); } } }
Izhod (vsebina output.txt):
Primer BufferedOutputStream v Javi
Primer 2: Uporaba metode flush().
Java import java.io.* ; public class FlushExample { public static void main ( String [] args ) { try { FileOutputStream fileOut = new FileOutputStream ( 'flush.txt' ); BufferedOutputStream bufferOut = new BufferedOutputStream ( fileOut ); bufferOut . write ( 'Hello' . getBytes ()); bufferOut . flush (); // ensures data is written immediately bufferOut . close (); System . out . println ( 'Flush example completed.' ); } catch ( IOException e ) { e . printStackTrace (); } } }
Izhod (vsebina flush.txt):
zdravo
Java import java.io.* ; public class LargeDataExample { public static void main ( String [] args ) { try { FileOutputStream fileOut = new FileOutputStream ( 'large.txt' ); BufferedOutputStream bufferOut = new BufferedOutputStream ( fileOut 8192 ); // custom buffer size for ( int i = 0 ; i < 1000 ; i ++ ) { bufferOut . write (( 'Line ' + i + 'n' ). getBytes ()); } bufferOut . close (); System . out . println ( 'Large data written successfully.' ); } catch ( IOException e ) { e . printStackTrace (); } } }
import java.io.* ; public class LargeDataExample { public static void main ( String [] args ) { try { FileOutputStream fileOut = new FileOutputStream ( 'large.txt' ); BufferedOutputStream bufferOut = new BufferedOutputStream ( fileOut 8192 ); // custom buffer size for ( int i = 0 ; i < 1000 ; i ++ ) { bufferOut . write (( 'Line ' + i + 'n' ). getBytes ()); } bufferOut . close (); System . out . println ( 'Large data written successfully.' ); } catch ( IOException e ) { e . printStackTrace (); } } }
Izhod (vsebina prvih nekaj vrstic large.txt):
Vrstica 0
vrstica 1
vrstica 2
...
Prednosti
- Hitrejša zmogljivost pisanja v primerjavi s tokovi brez medpomnjenja.
- Zmanjša število fizičnih zapisovalnih operacij.
- Omogoča učinkovito pisanje velikih podatkov.