简体   繁体   English

使用 java 使用 µ 等特殊字符解析 csv 文件

[英]Parse csv file with special character like µ using java

I have a situation where I have to read a CSV file which contains special character like 'µ'.我有一种情况,我必须阅读一个 CSV 文件,其中包含像“μ”这样的特殊字符。 This has to be done using java.这必须使用 java 来完成。 I am doing: BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(,"UTF-8"));我在做: BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(,"UTF-8"));

in windows it runs OK.在 windows 中运行正常。 But in redhat linux environment, it converts those special characters to '?'.但在 redhat linux 环境中,它会将这些特殊字符转换为“?”。 Any help is highly appreciated非常感谢任何帮助

Output written to System.out will be encoded using the "platform default encoding" which on linux is determined from locale environment variables (see output of locale command), and those in turn are set in user or system level configuration files.写入System.out的 Output 将使用“平台默认编码”进行编码,该编码在 linux 上由语言环境变量确定(参见locale或用户系统级配置文件)。

On server installations, the default encoding is often ASCII .在服务器安装上,默认编码通常是ASCII "µ" is not an ASCII character so it will be converted to "?" "µ" 不是 ASCII 字符,所以它会被转换为 "?" when it is printed.打印时。

There are a couple of ways to change the default encoding:有几种方法可以更改默认编码:

  • Set the Java file.encoding system property when you run your program, eg运行程序时设置 Java file.encoding系统属性,例如

    java -Dfile.encoding=utf-8 yourprogram
  • Set LC_CTYPE env variable before you run your program, for example:在运行程序之前设置 LC_CTYPE 环境变量,例如:

     export LC_CTYPE=en_US.UTF-8 java yourprogram
  • Those methods also change the default encoding for input and file names etc. You can change the encoding specifically for System.out with Java code:这些方法还会更改输入和文件名等的默认编码。您可以使用 Java 代码专门为System.out更改编码:

     PrintStream originalOut = System.out; // in case you need it later System.setOut(new PrintStream(System.out, true, "utf-8"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM