<<< DataOutputStream example: two ways to write a binary string | Index | Creating writeable file with Files.newOutputStream( ) method >>> |
import java.nio.file.*; import java.io.*; public class UnicodeInput { public static void main(String[] args) { try { DataInputStream in = new DataInputStream( new BufferedInputStream( new FileInputStream("myfile.dat"))); // get total bytes int totalBytes = in.available(); // use the readUTF method String string1 = in.readUTF(); int readSize1 = totalBytes - in.available(); System.out.println("readUTF reads " + readSize1 + " bytes."); // use the readChar method int readSize2 = 0; String string2 = ""; int charCount = in.available() / 2; for (int i = 0; i < charCount; i++) { string2 += in.readChar(); readSize2 += 2; } System.out.println("readChar reads " + readSize2 + " bytes.\n"); } catch ( IOException ex ) { } } }//class UnicodeInput
Resulting output from the binary strings
readUTF reads 25 bytes. readChar reads 46 bytes.
<<< DataOutputStream example: two ways to write a binary string | Index | Creating writeable file with Files.newOutputStream( ) method >>> |