Read the UTF-16 characters from the file and save them as UTF-8

I have a Person pojo, with a name attribute, which I store in my database in the table of the relevant persons. My db server is MySQL with utf-8 set as the standard server encoding, the person table is the InnoDB table that was also created using utf-8 as the default encoding, and my db connection string indicates utf-8 as the encoding connections.

I need to create and save new Personj pojos by reading their names from the txt file (persons.txt) that contains the name on each line, but the file encoding is UTF-16.

persons.txt

John

Μαρία

Helène

etc..

Here is a sample code:

PersonDao dao = new PersonDao();
File file = new File("persons.txt");
BufferedReader reader = new BufferedReader(
                        new InputStreamReader(new FileInputStream(file), "UTF-16"));
String line = reader.readLine();
while (line!=null) {
    Person p = new Person();
    p.setName(line.trim());
    dao.save(p);
    line = reader.readLine();
}

To summarize, I read string characters as utf-16, save them in local variables and save them as utf-8.

: - ? , ? , - utf-16 → utf-8?

+5
2

InputStreamReader (UTF-16 ) (.. char, String), UTF-16, .

String JDBC, ( MySQL , ).

( MySQL) , , UTF-8 UTF-16 .

+5

UTF-8 UTF-16 ( ), , , ( dao.save() ).

+2

All Articles