目的是从数据库读取并将记录写入文件。在IntelliJ IDEA中运行代码时,它将写入与数据库内容相同的Unicode字符。但是,当我构建工件(Jar File)并在Windows中运行它时,输出文件显示问号字符'?'。而不是正确显示数据库内容。换句话说,尽管英文字符和数字正确显示,但非英文字符(例如波斯字符,阿拉伯语或...)出现问题
Java代码的相关部分:
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile.txt , true), "cp1256"));
while (resultSet.next()) {
try {
singleRow = resultSet.getString("CODE") + "|"
+ resultSet.getString("ACTIVITY") + "|"
+ resultSet.getString("TEL") + "|"
+ resultSet.getString("ZIPCD") + "|"
+ resultSet.getString("ADDR");
} catch (Exception e) {
LogUtil.writeLog(Constants.LOG_ERROR, e.getMessage());
}
out.write(singleRow + System.getProperty("line.separator"));
}
通过运行IntelliJ IDEA DEBUG模式输出文件内容:
130143|Active|ابتداي بلوار ميرداماد،کوچه سوم پلاک پنج|524|35254410
190730|Active|خیابان زیتون، بین انوشه و زیبا پلاک یک|771|92542001
通过运行相应的JAR文件来输出文件内容:
130143|Active|35254410|524|??? ? ??? ??????? ????? ????
190730|Active|92542001|771|????? ??? ??????? ????? ??? ??
您能告诉我程序有什么问题吗?
您必须按照以下步骤更改代码:
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile.txt , true), StandardCharsets.UTF_8));
while (resultSet.next()) {
try {
singleRow = resultSet.getString("CODE") + "|"
+ resultSet.getString("ACTIVITY") + "|"
+ resultSet.getString("TEL") + "|"
+ resultSet.getString("ZIPCD") + "|"
+ resultSet.getString("ADDR") ;
} catch (Exception e) {
LogUtil.writeLog(Constants.LOG_ERROR, e.getMessage());
}
byte[] bytes = singleRow.getBytes(StandardCharsets.UTF_8);
String utf8EncodedString = new String(bytes, StandardCharsets.UTF_8);
out.write(utf8EncodedString + System.getProperty("line.separator"));
}
String.getBytes()使用系统默认字符集。您可以通过以下方式查看环境字符集:
System.out.println("Charset.defaultCharset="+ Charset.defaultCharset());
当从运行的IntelliJ,系统默认字符集的IntelliJ从环境中获取。
当从JAR运行文件,系统默认字符集是从操作系统(末尾解释)截取。
由于Windows和IntelliJ环境的字符集不同,您将获得不同的输出。
强烈建议在将字节转换为字符串时,明确指定“ ISO-8859-1”或“ US-ASCII”或“ UTF-8”或任何所需的字符集(反之亦然)
singleRow.getBytes(StandardCharsets.UTF_8)
看到这个链接更多的信息
什么是Windows-1252和Windows-1256?
Windows-1252
Windows-1252或CP-1252(代码页1252)是一个单字节(0-255)字符。拉丁字母的编码,默认情况下在Microsoft Windows的旧版组件中用于英语和许多欧洲语言,包括西班牙语,法语和德语。前128个代码(0-127)与标准ASCII代码相同。其他代码(128-255)取决于系统语言(西班牙语,法语,德语)。
Windows-1256
Windows-1256是一个代码页,用于在Microsoft Windows下编写阿拉伯语(以及其他可能使用阿拉伯语脚本的其他语言,如波斯语和乌尔都语)。这些是法语中使用的Windows-1252拉丁字符,因为这种欧洲语言在北非的前法国殖民地中具有一定的历史意义。这样,在使用Windows 1256时就可以混用法语和阿拉伯语文本,而无需进行代码页切换(但是,不包括带有变音符号的大写字母)。
使用Unicode(波斯)字符时该怎么办?
由于存在一些具有类似符号的不同字符,例如波斯语中的“ی”和“ي”,因此此编码将用“ي”(U + 064a)替换“ی”(U + 06cc),因为Windows-1256没有U + 06cc字符。
对于波斯语,使用Windows-1256的状态使用UTF-8编码来避免编码问题。请考虑Windows-1256仅使用1个字节,而UTF-8则占用更多字节(1到4个字节)。这些编码的比较在此处
如何更改Windows默认字符集?
现在,在Microsoft Windows上Windows-1252是大多数西方国家Windows系统使用的默认编码。
要将Microsoft Windows默认字符集更改为合适的Unicode,请遵循以下步骤。
如果您对波斯语进行如下更改,则默认字符集将更改为Windows-1256
如何更改特定的软件字符集(某些用于编程)?
您必须按照说明更改特定的软件Unicode。
3- For IntelliJ files Open the desired file for editing. From the main menu, select File | File encoding or click the file encoding on the status bar. Select the desired encoding from the popup. If or is displayed next to the selected encoding, it means that this encoding might change the file contents. In this case, IntelliJ IDEA opens a dialog where you can decide what you want to do with the file: choose Reload to load the file in the editor from disk and apply encoding changes to the editor only, or choose Convert to overwrite the file with the encoding of your choice.
4-IntelliJ控制台输出编码 IntelliJ IDEA使用“设置/首选项”对话框Ctrl + Alt + S的“文件编码”页面中定义的IDE编码创建文件。您可以使用系统默认值,也可以从可用编码列表中选择。默认情况下,此编码会影响控制台输出。如果希望控制台输出的编码与全局IDE设置不同,请配置相应的JVM选项:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句