java - Why does writeInt(int v) show as symbols in file but writeBytes(String s) shows properly in the file? -
i trying understand why when use code (using writeint(int v)
) symbols in file created.
public class write { public static void main(string[] args) { try { //create fileoutputstream object fileoutputstream fos = new fileoutputstream("v.txt"); dataoutputstream dos = new dataoutputstream(fos); int str = 2323; dos.writeint(str); dos.close(); } catch (ioexception e) { system.out.println("ioexception : " + e); } } }
but when use writebytes(string s)
or writeutf(string s)
correct string written in file. have read writebytes
converts primitive data byte
sequence of bytes why don't symbols getting using previous code using method?
public class write { public static void main(string[] args) { try { //create fileoutputstream object fileoutputstream fos = new fileoutputstream("v.txt"); dataoutputstream dos = new dataoutputstream(fos); string str = "hello"; dos.writebytes(str); dos.close(); } catch (ioexception e) { system.out.println("ioexception : " + e); } } }
also have read writebytes
neglects 8 high-order bits of each character, don't see difference in string entered , in file can explain mean neglecting 8 bits?
the result expected. number 1094861636 happens 41424344 in hexadecimal , ascii codes a, b, c , d happens 41, 42, 43, 44 in hexadecimal.
what writeint
break down number binary format , output bytes (8 binary "digits" chunks). hexadecimal nifty because each hexadecimal "digit" happens correspond 4 binary "digits".
what want use writestr(integer.tostring(yournumber))
instead. integer.tostring
converts integer human readable form, while writeint
produces more computer readable.
Comments
Post a Comment