简体   繁体   中英

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 :

Hello™ how ru ®

but when they are displayed in the output they are displayed as

Hello how ru

Can you tell me what changes should I make to my txt file so that it starts displaying the data accordingly.

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. InputStreams are just streams of bytes, they do not differentiate between encodings. 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")); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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