Search code examples
javajtablebyteasciirandomaccessfile

How to stop my output file from having Chinese characters?


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...?


Solution

  • 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+" ");