"Halo, Amigo! Dina iki kita bakal sepisan maneh dig menyang carane InputStream lan OutputStream bisa. Panjelasan dhisikan ana bener simplistic sethitik. Iki ora antarmuka. Padha kelas abstrak, lan malah duwe sawetara cara dipun ginakaken. Ayo goleki metode sing diduweni: "

Metode InputStream Apa metode kasebut
int read(byte[] buff);
Cara iki langsung maca blok byte menyang buffer ( byte array ), nganti buffer kebak utawa nganti sumber ora duwe bita maneh kanggo maca.
Cara kasebut ngasilake jumlah bita sing bener diwaca (sing bisa kurang saka dawane array)
int read();
Cara iki maca siji bait lan ngasilake. Asil wis widened kanggo int kanggo katon. Yen ora ana bita maneh sing diwaca, metode kasebut ngasilake -1.
int available();
Cara iki ngasilake jumlah bita sing durung diwaca (kasedhiya).
void close();
Cara iki "nutup" stream. Sampeyan nelpon iki yen sampeyan wis rampung nggarap stream.
Obyek banjur nindakake operasi housekeeping needed kanggo nutup file, etc.
Ing jalur iki, sampeyan ora bisa maca data liyane saka stream.

"Dadi kita bisa maca ora mung siji bait, nanging uga kabeh blok?"

"Pancen."

"Apa kita uga bisa nulis kabeh blok?"

"Ya, priksa:"

Metode OutputStream Apa metode kasebut
void write(int c);
Cara iki nulis siji bait. Jinis int disingkat dadi bait. Sisih ekstra mung dibuwang.
void write(byte[] buff);
Cara iki nulis blok byte.
void write(byte[] buff, int from, int count);
Cara iki nulis bagean saka blok byte. Iki digunakake ing kasus nalika array byte bisa uga ora diisi kabeh.
void flush();
Yen aliran internal nyimpen data sing durung ditulis, cara iki meksa supaya bisa ditulis.
void close();
Cara iki "nutup" stream. Sampeyan nelpon iki yen sampeyan wis rampung nggarap stream.
Obyek banjur nindakake operasi housekeeping needed kanggo nutup file, etc. Sampeyan ora bisa nulis data kanggo stream maneh, lan flush diarani kanthi otomatis.

"Apa sing bakal katon kode nyalin file yen kita maca kabeh blok sekaligus tinimbang siji bita?"

"Hmm. Kaya kiye:"

Nyalin file ing disk
public static void main(String[] args) throws Exception
{
 //Create a stream to read bytes from a file
 FileInputStream inputStream = new FileInputStream("c:/data.txt");
 //Create a stream to write bytes to a file
 FileOutputStream outputStream = new FileOutputStream("c:/result.txt");

  byte[] buffer = new byte[1000];
 while (inputStream.available() > 0) //as long as there are unread bytes
 {
  //Read the next block of bytes into buffer, and store the actual number of bytes read in count.
  int count = inputStream.read(buffer);
  outputStream.write(buffer, 0, count); //Write a block (part of a block) to the second stream
 }

 inputStream.close(); //Close both streams. We don't need them any more.
 outputStream.close();
}

"Aku ngerti kabeh babagan buffer, nanging apa variabel count iki?"

"Nalika kita maca pamblokiran data paling anyar saka file, kita bisa njaluk, ngomong, 328 bita tinimbang 1000. Dadi nalika kita nulis data, kita kudu nuduhake yen kita ora nulis kabeh blok-mung 328 pisanan. bita."

Nalika maca pamblokiran pungkasan, cara maca bakal ngasilake jumlah bita sing bener diwaca. 1000 saben-saben kita maca blok, kajaba blok pungkasan, nalika entuk 328.

Dadi nalika kita nulis pemblokiran, kita nunjukaké sing ora kabeh bait ing buffer kudu ditulis, mung 328 (IE nilai disimpen ing variabel count).

"Saiki kabeh wis jelas. Matur nuwun, Ellie."