Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to avoid garbled codes in Chinese when opening UTF-8 csv files by Java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

The main content of this article is to explain "Java how to avoid UTF-8 csv file opening Chinese garbled", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Java how to avoid UTF-8 csv file opening Chinese garbled" bar!

Because of the limited conditions, only these versions have been tested, which shows that utf-16le is a more general coding format. The java code is attached below. Utf-16le encoding is used in the main method. Finally, the utf8 encoding method is called, and the csv files in two encoding formats are output:

Import java.io.*;/** * Created by zhaozhi on 15-5-29. * / public class TestCSV {public static String join (String [] strArr, String delim) {StringBuilder sb = new StringBuilder (); for (String s: strArr) {sb.append (s); sb.append (delim);} String ret; if (strArr.length > 1) {ret = sb.substring (0, sb.length ()-1);} else {ret = sb.toString ();} return ret } public static void main (String [] args) throws Exception {String [] heads = {"date", "product", "number of orders"}; String [] [] rows = {{"20150228", "Android", "23"}, {"20150301", "web", "34"}}; byte [] bom = {(byte) 0xFF, (byte) 0xFE} String fname = "d:\\ utf-16le.csv"; BufferedOutputStream bo = new BufferedOutputStream (new FileOutputStream (fname)); bo.write (bom); bo.write (join (heads, "\ t"). GetBytes ("utf-16le")); bo.write ("\ n" .getBytes ("utf-16le")); for (String [] row: rows) {bo.write (join (row, "\ t"). GetBytes ("utf-16le")) Bo.write ("\ n" .getBytes ("utf-16le"));} bo.close (); UTF8 ();} public static void UTF8 () throws IOException {String line = "Chinese, title, 23"; OutputStream os = new FileOutputStream ("d:/utf-8.csv"); os.write (239); / / 0xEF os.write (187); / / 0xBB os.write (191) / / 0xBF PrintWriter w = new PrintWriter (new OutputStreamWriter (os, "UTF-8")); w.print (line); w.flush (); w.close ();}} excel version of the additional package coding test results so far, I believe that "Java how to avoid UTF-8 csv file opening Chinese garbled" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report