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

  • Home
  • SEARCH
  • 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 8915699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:02:00+00:00 2026-06-15T05:02:00+00:00

i want to convert the content to utf-8 charset before i return the string

  • 0

i want to convert the content to utf-8 charset before i return the string in following method:

public static String getContentResult(URL url) throws IOException{
    InputStream in = url.openStream();
    StringBuilder sb = new StringBuilder();

    byte [] buffer = new byte[4096];

    while(true){
        int byteRead = in.read(buffer);
        if(byteRead == -1)
            break;
        for(int i = 0; i < byteRead; i++){
            sb.append((char)buffer[i]);
        }
    }
    in.close();
    return sb.toString();
}

How can i do that?

Thanks!

  • 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-15T05:02:01+00:00Added an answer on June 15, 2026 at 5:02 am

    You don’t want to convert to UTF-8. You want (I believe) to interpret the incoming stream of data as UTF-8.

    Options:

    • Create an InputStreamReader wrapping your incoming stream, specifying UTF-8, and read blocks of characters at a time, appending to a StringBuilder

      StringBuilder builder = new StringBuilder();
      char[] buffer = new char[4096];
      InputStreamReader reader = new InputStreamReader(in, "UTF-8");
      int charsRead;
      while ((charsRead = reader.read(buffer)) != -1) {
          builder.append(buffer, 0, charsRead);
      }
      
    • Use Guava to read the whole data as a byte array, then convert it in one go:

      byte[] data = BytesStreams.toByteArray(in);
      return new String(data, Charsets.UTF_8);
      

    In either case, you should use a finally block to close the stream, so that you close it even if an exception is thrown.

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

Sidebar

Related Questions

I've got an std::string content that I know contains UTF-8 data. I want to
I have lots of UTF-8 content that I want inserted into the URL for
I have a string that has HTML formatted content. Now I want to convert
I want to convert a string to formatted text in C# in WPF application,
I want to convert all the URLs in a javascript string to links, in
I want to convert an java string containing xml to a w3c dom document
i want to make a util method to convert from map to list to
i want to convert text links in my content page into active links using
I want to convert this array content: $colorList[0] = red; $colorList[1] = green; $colorList[2]
I want to convert the mysql database table contents to an Excel(.xls) or comma

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.