Created
October 29, 2022 16:36
-
-
Save rmg007/4b339bca54565d552a6846a9f14b7c27 to your computer and use it in GitHub Desktop.
BufferedInputStream BufferedOutputStream Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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