0

I am building a sample app that converts an conversation from an iOS sms database file into a readable html page.

But in the in the console and the html page, emojis show up as '?' but in the DB Browser they show up alright.

Example of the HTML preview: Example of the HTML page

And my code:

public static void main(String[] args) throws SQLException, IOException {

     String url = "jdbc:sqlite:C:/test/sms2.db";

     Connection conn = DriverManager.getConnection(url);

     Statement s = conn.createStatement();

     ResultSet rs = s.executeQuery("SELECT handle_id,is_from_me,text FROM message");

     File f = new File("C:\\Users\\theda\\Documents\\Messages\\convo\\+1**********.html");
     if(!f.exists())
         f.createNewFile();
     FileWriter w = new FileWriter(f);
     WriterOutputStream write = new WriterOutputStream(w, Charset.forName("utf8"));

     w.append("<link rel='stylesheet' type='text/css' href='message.css'>");
     w.append("<body>");

     while(rs.next()){
         if(rs.getInt("handle_id") == 3){
             String st = " ";
             String fromMe = " ";
             if(rs.getInt("is_from_me") == 1)
                 fromMe = "true";
             else
                 fromMe = "false";
             st = "<div class='message' data-outgoing='" + fromMe + "'>" + rs.getString("text") + "</div><br>";

             System.out.println(st);
             w.append(st);
         }
    }

     w.append("</body>");
     w.close();

}
DarkLlama
  • 25
  • 7

1 Answers1

1

Make sure your HTML page is using the correct charset in the meta tags:

<meta charset="utf-8" />

In general, it's also a good idea to encode your special characters as HTML entities when putting them in the HTML. According to this answer, you can do this with Java's StringEscapeUtils.escapeHtml() method.

John Ellmore
  • 2,209
  • 1
  • 10
  • 22
  • Thank you for the answer, but now they show up as � on the html but they do say '' and similar in the console – DarkLlama Nov 21 '17 at 20:54
  • I'd advise that you run your page output through the [W3 Validator](https://validator.w3.org/). It may identify some encoding issues on your page. – John Ellmore Nov 21 '17 at 20:56