Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8503573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:47:33+00:00 2026-06-11T01:47:33+00:00

I have following code which reads data from text file and prints to browser.

  • 0

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

        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:47:35+00:00Added an answer on June 11, 2026 at 1:47 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which reads in the follow file, append a \r\n
I have application reads in data from text file. Recently I realized that I
I have the following C# code, which pulls data from a database & populates
I have following code which works for radio buttons but need to be changed
I have the following code which definitely returns a proper data result if I
Let's say I want to have a function which reads data from the SerialPort
I have a simple application that reads data in small strings from large text
I have the following code which gets the user's current zipcode and displays it
I want to know is below code correct ? I have following code which
I have the following code which is working, I was wondering if this can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.