Created
March 27, 2020 16:53
-
-
Save affandes/3f76ea6cb6c95ba10585cebf1a2f921f to your computer and use it in GitHub Desktop.
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.*; | |
import java.util.Arrays; | |
public class ByteStreamVsCharStream { | |
public static void main(String[] args) { | |
// Create source | |
String source = "Affandes"; | |
// Create Byte Stream & Character Stream | |
InputStream byteStream = new ByteArrayInputStream(source.getBytes()); | |
StringReader characterStream = new StringReader(source); | |
// Create buffer | |
byte byteBuff[] = new byte[4]; | |
char charBuff[] = new char[4]; | |
// Read | |
try { | |
byteStream.read(byteBuff); | |
characterStream.read(charBuff); | |
} catch (IOException e) { | |
// Error handling | |
} | |
System.out.println(Arrays.toString(byteBuff)); | |
System.out.println(Arrays.toString(charBuff)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment