1

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);

        }
abcuser
  • 45
  • 7

2 Answers2

0

Your file may not be in UTF-8. I would read all file concatenating all bytes in a array, then create an string with it and then I would print it.

0

try to output something manually something like

out.print("υτφ-8 chars");

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

FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis, "UTF8");

NEW UPDATE

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        File f = new File("C:\\TEST.TXT");
        FileInputStream fis = new FileInputStream(f);
        InputStreamReader isr  = new InputStreamReader(fis, "UTF-8");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Expires", "-1");
        response.setHeader("Cache-Control", "no-cache");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        out = response.getWriter();
        for (int i = isr.read(), k = 0; i != -1; i = isr.read(), ++k) {
            if (i == '\n') {
                out.print("</BR>");
            } else {
                out.write(i);
            }
            out.flush();
        }
    } finally {
        out.close();
    }
}

The above code should work fine, i have tested it

Community
  • 1
  • 1
MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
  • MaVRoSCy : its working (fis and isr) but it also removes hyphen(-) from "abc - abc".. any more solution ?? – abcuser Sep 06 '12 at 10:28
  • MaVRoSCy : f = new File(URLDecoder.decode(filePathStr), URLDecoder.decode(fileName)); fis = new FileInputStream(f); isr = new InputStreamReader(fis,"UTF-8"); 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 = isr.read(), k = 0; i != -1; i = isr.read(),++k) { if (i == '\n') out.print(""); else out.write((byte) i); out.flush(); } – abcuser Sep 06 '12 at 11:11
  • MaVRoSCy :: where to see answer ?? – abcuser Sep 06 '12 at 11:28
  • it didn't work for me either till the time i removed out.write((byte) i); and set it as the example above. Please make sure that we have identical code – MaVRoSCy Sep 06 '12 at 12:18
  • @nehal if the answer is what you need you can up-vote it and accept it – MaVRoSCy Sep 06 '12 at 12:33