BufferedInputStream akıştan veri okumak için kullanılır.
BufferedOutputStream ise veri yazmak için kullanılır.
Öncelikle arkadaşlar bi veriyi düşünün bunu diskten tek tek okumak gerçekten çok etkisiz ve gereksiz bir yoldur.Bunu hızlı bir şekilde yapmak için buffer kullanmalıyız ; instead of reading one byte at a time, you read a few thousand bytes at once, and put them in a buffer, in memory. Then you can look at the bytes in the buffer one by one.
BufferedOutputStream da yine write methodu kullanıyoruz , unutmayın OutputStream ile başlayanlar bu abstract classı extends ediyodular demiştim bunun içerisindeki method ve fieldlar ne varsa hepsini de kendi classlarına dahil ediyolar , aynı şekilde InputStream classları için de.
Üstteki yazıda her şey açıklanmıştır. Bir örnek verelim hemen.
BufferedOutputStream Örneği :
import java.io.*; public class MyFile {<span style="color:#cc7832">public static void </span><span style="color:#ffc66d">main</span>(String[] args) <span style="color:#cc7832">throws </span>FileNotFoundException { <span style="color:#808080">// Dosyaya yazmak için
try(OutputStream fos = new FileOutputStream("/home/j2guar/Desktop/myName.txt"); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fos)) { String myName = "Hello , i'm kerem"; byte[] myNameBytes = myName.getBytes(); bufferedOutputStream.write(myNameBytes); } catch (IOException e) { e.printStackTrace(); } } }
BufferedInputStream Örneği :
import java.io.*; public class MyFile {
<span style="color:#cc7832">public static void </span><span style="color:#ffc66d">main</span>(String[] args) <span style="color:#cc7832">throws </span>FileNotFoundException { <span style="color:#808080">// Okumak için
try (InputStream fis = new FileInputStream("/home/j2guar/Desktop/myName.txt"); BufferedInputStream bufferedInputStream = new BufferedInputStream(fis) ) { int i; while((i=bufferedInputStream.read())!=-1){ System.out.print((char)i); }
} <span style="color:#cc7832">catch </span>(IOException e) { e.printStackTrace()<span style="color:#cc7832">;
}
}}