1

I am loading a file,

getting a line from it

and putting it in a jTable

the jTable shows some of my charcters as square boxes.

answers I found online:

1.

  • it's how the file is opened, so I changed
  • BufferedReader bf = new BufferedReader(new FileReader(filename));
  • into
  • BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "UTF8"));
  • I also tried Charset instead of the string

2.

  • it's the font, so I tried
  • jTable1.setFont(new Font("Times New Roman", Font.BOLD, 12));
  • I tried other fonts, like Arial, David, ...

Can you think of any other reason??

By nachokk's request, here's my code:

        int linecount = 0;
        String line = null;
        BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "UTF8"));
        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();

        while ((line = bf.readLine()) != null) {
            linecount++;
            model.addRow(new Object[]{filename, linecount, line});
        }
Amir.F
  • 1,911
  • 7
  • 29
  • 52
  • 3
    Can you post an example reproducing the problem you are facing? – nachokk Mar 24 '14 at 23:03
  • updated post with the code – Amir.F Mar 25 '14 at 09:21
  • What's the Unicode code point of the character that you can't see? Use [String.codePointAt](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#codePointAt(int)) to find out. – Aaron Digulla Mar 25 '14 at 09:51
  • it gives me a replacement charcter 65533 at every location, http://www.fileformat.info/info/unicode/char/0fffd/index.htm, except spaces which are 32 – Amir.F Mar 25 '14 at 13:30

1 Answers1

0

You get a box on the screen when you use a font which doesn't contain the necessary glyphs (i.e. it doesn't know how to render certain Unicode character codes).

If Java couldn't read the file (i.e. encoding issues), you should see ? instead.

If you need a quick test, open a text editor that allows to select the fond and then your OS's Unicode input method to enter some Unicode values (or, if you have a file, just try to open it).

Note that some word processors notice when a glyph is missing and they automatically fall back to a font which contains the glyph.

[EDIT]

it gives me a replacement character 65533 at every location

That means you use the wrong encoding to read the file. Find out what the correct encoding is (ask the people who create it, for example) and use that instead of UTF-8

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • I tried different fonts, but no success, also notice Aaron wrote me a legitimate comment, which made me realize that it still might be the way the file is loaded, since all of those characters are "replacement character"s, even before it gets to the jTable – Amir.F Mar 25 '14 at 16:46
  • In that case, use the correct encoding. See my edits. – Aaron Digulla Mar 25 '14 at 16:58
  • I created the file. I just saved the file again in UTF-8, and it still doesn't work. was thinking of implementing this http://stackoverflow.com/questions/499010/java-how-to-determine-the-correct-charset-encoding-of-a-stream, but it seems pointless – Amir.F Mar 25 '14 at 18:07
  • Look into the file with a hex editor to see what it actually contains. Use the rules in http://en.wikipedia.org/wiki/UTF-8 to decode one or two characters manually to see if the data makes sense. – Aaron Digulla Mar 26 '14 at 08:54