Reputation: 55
I'm trying to write into a RandomAccessFile by taking data from cells from a specific jtable. I convert the strings into bytes and then I implement the .write() function in order to write to the output file.
I made it so that I will be able to see what my output should be by printing bytes into the console. However when I check with my output file, I see Chinese character.
String data = (String) table.getModel().getValueAt(r,c).toString();
byte[] BytedString = data.getBytes();
file.write(BytedString);
String s = new String(BytedString);
System.out.print(s+" ");
Did I do something wrong...?
Upvotes: 0
Views: 1166
Reputation: 10707
in your code instead of using:
String s = new String(BytedString);
use:
String s = new String(BytedString, "UTF-8");
or
new String(BytedString, "US-ASCII");
new String(BytedString, "ISO-8859-1");
depending on your platform encoding.
String data = (String) table.getModel().getValueAt(r,c).toString();
byte[] BytedString = data.getBytes();
String s = new String(BytedString, "UTF-8");
file.writeChars(s);
System.out.print(s+" ");
Upvotes: 2