如何阅读 SJIS 文件中的这段文字①②?
How to read this text ①② in a SJIS file?
如何获取这些字符①②
?
我正在使用 Java 1.6 读取包含这些字符的 csv 文件(采用 SJIS 编码),我只在 运行 程序时才返回此 ��
。
public class Example {
public static void main(String[] args) throws IOException {
StringBuffer buffer = new StringBuffer();
FileInputStream fis = new FileInputStream(new File("examples/input.csv"));
InputStreamReader isr = new InputStreamReader(fis, "SJIS");
Reader in = new BufferedReader(isr);
int ch;
while ((ch = in.read()) > -1) {
buffer.append((char)ch);
}
in.close();
System.out.println(buffer.toString());
}
}
输入
input.csv
的内容:
"備考"
①②ランプ
实际输出
�@�Aランプ
预期输出
"①②ランプ"
在我看来,您可能需要设置程序用于打印的 console/terminal 的编码。
这可能不是您要找的答案,但如果您使用的是 Eclipse,则可以尝试以下操作。根据您的问题,输出是正确的。
日食IDE
在 Eclipse 中,如果单击 运行 按钮旁边的向下箭头,您应该会看到 "Run Configurations..." 选项。如果您 select "Common" 选项卡,您应该会看到一个标记为 "Encoding" 的表单字段集。 Select "Other" 单选按钮并将 selection 更改为 "UTF-8"。
工作示例
我稍微修改了代码,通过项目加载文件。我还使用 Character.toChars(ch)
而不是 (char) ch
作为解析字符的更可靠的方法。 Character.toChars
函数"Converts the specified character (Unicode code point) to its UTF-16 representation stored in a char array."
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URISyntaxException;
public class App {
public static void main(String[] args) {
try {
StringBuffer buffer = new StringBuffer();
FileInputStream fis = loadResource("resources/input.csv");
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
Reader in = new BufferedReader(isr);
int ch;
while ((ch = in.read()) > -1) {
buffer.append(Character.toChars(ch));
}
in.close();
System.out.println(buffer.toString());
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
private static final FileInputStream loadResource(String name) throws FileNotFoundException, URISyntaxException {
return new FileInputStream(new File(App.class.getClassLoader().getResource(name).toURI()));
}
}
Shift-JIS字符集不包含字符①和②
您的文件未以标准 Shift-JIS, the encoding you get in Java under the name SJIS
. It's actually Windows code page 932 保存,这是 Microsoft 特定的 Shift-JIS 扩展,确实 包括①②。您可以使用名称 windows-932
.
在 Java 中获取此编码
如何获取这些字符①②
?
我正在使用 Java 1.6 读取包含这些字符的 csv 文件(采用 SJIS 编码),我只在 运行 程序时才返回此 ��
。
public class Example {
public static void main(String[] args) throws IOException {
StringBuffer buffer = new StringBuffer();
FileInputStream fis = new FileInputStream(new File("examples/input.csv"));
InputStreamReader isr = new InputStreamReader(fis, "SJIS");
Reader in = new BufferedReader(isr);
int ch;
while ((ch = in.read()) > -1) {
buffer.append((char)ch);
}
in.close();
System.out.println(buffer.toString());
}
}
输入
input.csv
的内容:
"備考"
①②ランプ
实际输出
�@�Aランプ
预期输出
"①②ランプ"
在我看来,您可能需要设置程序用于打印的 console/terminal 的编码。
这可能不是您要找的答案,但如果您使用的是 Eclipse,则可以尝试以下操作。根据您的问题,输出是正确的。
日食IDE
在 Eclipse 中,如果单击 运行 按钮旁边的向下箭头,您应该会看到 "Run Configurations..." 选项。如果您 select "Common" 选项卡,您应该会看到一个标记为 "Encoding" 的表单字段集。 Select "Other" 单选按钮并将 selection 更改为 "UTF-8"。
工作示例
我稍微修改了代码,通过项目加载文件。我还使用 Character.toChars(ch)
而不是 (char) ch
作为解析字符的更可靠的方法。 Character.toChars
函数"Converts the specified character (Unicode code point) to its UTF-16 representation stored in a char array."
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URISyntaxException;
public class App {
public static void main(String[] args) {
try {
StringBuffer buffer = new StringBuffer();
FileInputStream fis = loadResource("resources/input.csv");
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
Reader in = new BufferedReader(isr);
int ch;
while ((ch = in.read()) > -1) {
buffer.append(Character.toChars(ch));
}
in.close();
System.out.println(buffer.toString());
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
private static final FileInputStream loadResource(String name) throws FileNotFoundException, URISyntaxException {
return new FileInputStream(new File(App.class.getClassLoader().getResource(name).toURI()));
}
}
Shift-JIS字符集不包含字符①和②
您的文件未以标准 Shift-JIS, the encoding you get in Java under the name SJIS
. It's actually Windows code page 932 保存,这是 Microsoft 特定的 Shift-JIS 扩展,确实 包括①②。您可以使用名称 windows-932
.