Srinivas
Srinivas

Reputation: 147

How to read JTable data into ArrayList having special characters(like Chinese)?

I have a Jtable with Chinese characters, while I am trying to fetch the data into Array List ,It is showing as ???????,Displaying it in eclipse console is Ok ,but displaying it in html file is making tricky.

package test.java;
import javax.swing.*;    
public class TableExample 
{    
    JFrame f;   
    JTable jt ;
    public JTable m1() 
    {    
        f=new JFrame();    
        String data[][]={ {"100","你叫什么名字? ","1234"},    
                {"111","プードル","4567"},    
                {"222","유모차","343124"}};    
        String column[]={"ID","NAME","SALARY"};       
        jt=new JTable(data,column);    
        jt.setBounds(30,40,200,300);          
        JScrollPane sp=new JScrollPane(jt);    
        f.add(sp);          
        f.setSize(300,400);    
        f.setVisible(true);   
        return jt;
    }  
}

Second program to read the data and to write that data into one HTML file,there I am unable to see my special characters

    package test.java;
import java.util.ArrayList;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JTable;
public class TableClass {
    public static void main(String[] args) 
    {
        ArrayList<String> numdata = new ArrayList<String>();    
        TableExample mainTable =  new TableExample();
        JTable table = mainTable.m1();
        for (int count = 0; count < table.getRowCount(); count++)
        {
            numdata.add(table.getValueAt(count, 1).toString());
        }

        FileWriter writer = null;
        try 
        {
            writer = new FileWriter("D:/Testing.html");
        } catch (IOException e) 
        {
            e.printStackTrace();
        } 
        for(String str: numdata) 
        {
            try 
            {
                writer.write(str);
            } catch (IOException e) 
            {
                e.printStackTrace();
            }
        }
        try {
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }   
    }
}

Upvotes: 0

Views: 236

Answers (1)

cello
cello

Reputation: 5486

Assuming your source code is saved in UTF-8 encoding, you need to specify this encoding as well when writing the html. Currently, you just write a bunch of characters to the html-file, without any proper html tags.

I've adapted your "writing to html" part of the code as follows:

try {
    FileWriter writer = new FileWriter("testing.html");
    writer.write("<html><head><meta charset=\"UTF-8\"></head><body>");
    for(String str: numdata) {
        writer.write("<p>" + str + "</p>");
    }
    writer.write("</body></html>");
    writer.close();
} catch (IOException e) {
    e.printStackTrace();
}

It first writes a proper html header with a meta-tag specifying that the content is in UTF-8. Then, it writes each string as a proper paragraph (<p>...</p>), and in the end, closes the html again correctly.

Upvotes: 0

Related Questions