I have following code which reads data from text file and prints to browser.
But when it gets data like “abc – abc ” then in browser it shows junk characters like ” ¬タモ”…
What could be the problem ?
fis points to text file. Read data from text file and write to browser.
thnx in advanced.
f = new File(URLDecoder.decode(filePathStr), URLDecoder.decode(fileName));
fis = new FileInputStream(f);
res.setHeader("Pragma", "no-cache");
res.setHeader("Expires", "-1");
res.setHeader("Cache-Control", "no-cache");
req.setCharacterEncoding("UTF-8");
res.setContentType("text/html;charset=UTF-8");
out = res.getWriter();
for (int i = fis.read(); i != -1; i = fis.read()) {
if (i == '\n')
out.print("</BR>");
else
out.write((byte) i);
}
try to output something manually something like
Do they print correctly? If not take a look at this article How to get UTF-8 working in Java webapps? and make sure you follow the 4 steps on how to display UTF-8 Chars in your webpage.
If they print correctly then the problem lies with the FileInputStream and how you read the file.
You can try the following
NEW UPDATE
The above code should work fine, i have tested it