简体   繁体   English

txt 文件中的特殊字符未在 InputStream 中传递

[英]Special character in txt file not being passed in the InputStream

I have an我有一个

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("templates/createUser/new-user.txt");

and the content of the new-user.txt is :并且new-user.txt的内容是:

Hello™ how ru ®你好™ 怎么 ru ®

but when they are displayed in the output they are displayed as但是当它们显示在输出中时,它们显示为

Hello how ru 你好...如何 ru...

Can you tell me what changes should I make to my txt file so that it starts displaying the data accordingly.你能告诉我应该对我的 txt 文件进行哪些更改,以便它开始相应地显示数据。

UPDATE更新

So here is the code :-所以这是代码:-

Handlebars handlebars = new Handlebars(); InputStream txtInputStream = this.getClass().getClassLoader() .getResourceAsStream("templates/createUser/new-user.txt"); Template textTemplate = handlebars.compileInline(IOUtils.toString(txtInputStream)); String emailText = textTemplate.apply(vars);

The problem does not lie in the InputStream object.问题不在于 InputStream 对象。 InputStreams are just streams of bytes, they do not differentiate between encodings. InputStreams 只是字节流,它们不区分编码。 The problem is you should use this as your reader:问题是你应该使用它作为你的读者:

Reader reader = new InputStreamReader(inputStream, "UTF-8");

as opposed to using this:而不是使用这个:

Reader reader = new InputStreamReader(inputStream); // does not specify encoding

You can then get the string with:然后,您可以使用以下命令获取字符串:

String theString = IOUtils.toString(inputStream, "UTF-8");

Edit: I did not realize you posted full code in the comments.编辑:我没有意识到你在评论中发布了完整的代码。 Just change your second to last line to:只需将倒数第二行更改为:

Template textTemplate = handlebars.compileInline(IOUtils.toString(txtInputStream, "UTF-8")); 

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

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