<<< DataOutputStream: writing binary strings | Index | DataInputStream example: two ways to read a binary string >>> |
import java.nio.file.*; import java.io.*; public class UnicodeOutput { public static void main(String[] args) { try { DataOutputStream out = new DataOutputStream( new BufferedOutputStream( new FileOutputStream("books.bin"))); // create a test string String testString = "This is a test string.\n"; // use the writeUTF method out.writeUTF(testString); int writeSize1 = out.size(); System.out.println( "writeUTF writes " + writeSize1 + " bytes."); // use the writeChars method out.writeChars(testString); int writeSize2 = out.size() - writeSize1; System.out.println( "writeChars writes " + writeSize2 + " bytes.\n"); out.close(); } catch ( IOException ex ) { ex.printStackTrace(); } } }//class UnicodeOutput
Resulting output
writeUTF writes 25 bytes. writeChars writes 46 bytes.
<<< DataOutputStream: writing binary strings | Index | DataInputStream example: two ways to read a binary string >>> |