0

I have a csv file. i need to display csv file content in html table using jsp. i had written a desired code using open csv. yes, it is sucessfully shown in html table. But some cryptic characters are displayd like " -?- 1 -?- " in the place of "- 1-" where those question marks are unecessary.

I had tried converting values to ansi in the following way to eliminate entry of cryptic characters :

csvReader = new CSVReader(reader);
int rowCount = 1;
while ((line = csvReader.readNext()) != null) {
%>
<tr>
<td class="heading" width="30"><%=rowCount%></td>
<%
for (int i = 0; i < line.length; i++) {
    if (rowCount == 1) {
    %>
    <td class="ex-headding" width="120"><strong><%=line[i]%></strong></td>
    <%
    } else {
        //this is the code to convert into ansi
        String utf = line[i];  
        byte[] data = utf.getBytes("ASCII");  
        line[i] = new String(data);
    %>
    <td><%=line[i]%></td>
    <%
    }
}
%>  

But issue still there . Is that the correct way of conversion into ANSI

vzamanillo
  • 9,905
  • 1
  • 36
  • 56

1 Answers1

0

Probably you have something like

<%@page pageEncoding="ISO-8859-1"%>

Which is the official Latin-1. Use:

<%@page pageEncoding="Windows-1252"%>

Which is the Windows Latin-1 with some more special characters. All browsers interprete ISO-8859-1 as Windows-1252 (also MacOSX, Linux), and HTML 5 even prescribes it this way. Only java is strict.

Hence you may keep in HTML:

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

The question mark is given, when the original Unicode character cannot be represented in the outputting character set.

Should the character still remain unrepresentable, switch to "UTF-8". That is definitely the future, driven by the multi-lingual world, and current Unicode base.


As that was not the cause: Check the encoding of the CSV file. For instance with JEdit. And then add the encoding to the CSVReader constructor.

That ensures we have correct Strings to begin with.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138