Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rmg007/4b339bca54565d552a6846a9f14b7c27 to your computer and use it in GitHub Desktop.
Save rmg007/4b339bca54565d552a6846a9f14b7c27 to your computer and use it in GitHub Desktop.
BufferedInputStream BufferedOutputStream Example
import java.io.*;
public class BufferedInputStreamBufferedOutputStreamExample {
// BufferedInputStream and BufferedOutputStream are efficient
// when reading and writing large files in chunks
static String filePath = "/Users/ryan/Desktop/file.txt";
public static void main(String[] args) throws IOException {
String word = "hello java";
byte[] bytes = word.getBytes();
//try( FileOutputStream fos = new FileOutputStream(filePath);
// BufferedOutputStream bos = new BufferedOutputStream(fos)){
// bos.write(bytes);
//}
try(FileInputStream fis = new FileInputStream(filePath);
BufferedInputStream bis = new BufferedInputStream(fis)) {
System.out.println((char)bis.read()); // h
System.out.println((char)bis.read()); // e
bis.mark(200);
System.out.println((char)bis.read()); // l
System.out.println((char)bis.read());
System.out.println((char)bis.read());
System.out.println((char)bis.read());
bis.reset();
System.out.println("after reset");
System.out.println((char)bis.read());
System.out.println((char)bis.read());
System.out.println((char)bis.read());
//System.out.println(new String(bis.readAllBytes()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment