将已知编码的文件转换为 UTF-8
我需要将文本文件转换为字符串,最后,我应该将其作为输入参数(类型为InputStream)放入IFile.create(Eclipse)。 正在寻找示例或如何做到这一点,但仍然无法弄清楚......需要您的帮助!
只是为了测试,我确实尝试将原始文本文件转换为使用此代码编码的 UTF-8,
FileInputStream fis = new FileInputStream(FilePath);
InputStreamReader isr = new InputStreamReader(fis);
Reader in = new BufferedReader(isr);
StringBuffer buffer = new StringBuffer();
int ch;
while ((ch = in.read()) > -1) {
buffer.append((char)ch);
}
in.close();
FileOutputStream fos = new FileOutputStream(FilePath+".test.txt");
Writer out = new OutputStreamWriter(fos, "UTF8");
out.write(buffer.toString());
out.close();
但即使认为最终的 *.test.txt 文件具有 UTF-8 编码,内部字符也已损坏。
I need to convert text file to the String, which, finally, I should put as an input parameter (type InputStream) to IFile.create (Eclipse).
Looking for the example or how to do that but still can not figure out...need your help!
just for testing, I did try to convert original text file to UTF-8 encoded with this code
FileInputStream fis = new FileInputStream(FilePath);
InputStreamReader isr = new InputStreamReader(fis);
Reader in = new BufferedReader(isr);
StringBuffer buffer = new StringBuffer();
int ch;
while ((ch = in.read()) > -1) {
buffer.append((char)ch);
}
in.close();
FileOutputStream fos = new FileOutputStream(FilePath+".test.txt");
Writer out = new OutputStreamWriter(fos, "UTF8");
out.write(buffer.toString());
out.close();
but even thought the final *.test.txt file has UTF-8 encoding, the characters inside are corrupted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
Charset
参数指定InputStreamReader
的编码。这也有效:
另请参阅:
InputStreamReader(InputStream in, Charset cs)
Charset.forName(String charsetName)
所以搜索我找到了所有这些链接:https://stackoverflow.com/search?q=java+detect+encoding
您可以在运行时通过
Charset 获取默认字符集(来自 JVM 运行的系统)。 defaultCharset()。
You need to specify the encoding of the
InputStreamReader
using theCharset
parameter.This also works:
See also:
InputStreamReader(InputStream in, Charset cs)
Charset.forName(String charsetName)
SO search where I found all these links: https://stackoverflow.com/search?q=java+detect+encoding
You can get the default charset - which is comes from the system the JVM is running on - at runtime via
Charset.defaultCharset()
.