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 8885537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:20:00+00:00 2026-06-14T21:20:00+00:00

I am getting this error: io.MalformedByteSequenceException: Invalid byte 2 of 2-byte UTF-8 sequence The

  • 0

I am getting this error:

io.MalformedByteSequenceException: Invalid byte 2 of 2-byte UTF-8 sequence

The solution is to read and write a file in UTF-8.

My code is:

InputStream input = null;
OutputStream output = null;
OutputStreamWriter bufferedWriter = new OutputStreamWriter( output, "UTF8");
input = new URL(url).openStream();
output = new FileOutputStream("DirectionResponse.xml");
byte[] buffer = new byte[1024];
for (int length = 0; (length = input.read(buffer)) > 0;) {
   output.write(buffer, 0, length);
}
BufferedReader br = new BufferedReader(new FileReader("DirectionResponse.xml" ));
FileWriter fstream = new FileWriter("ppre_DirectionResponse.xml");
BufferedWriter out = new BufferedWriter(fstream);

I’m reading a url and writing it to a file DirectionResponse.xml. Then reading DirectionResponse.xml and writing the same as ppre_DirecionResponse.xml for processing.

How do I change this so that reading and writing is done in UTF-8?

  • 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-14T21:20:01+00:00Added an answer on June 14, 2026 at 9:20 pm

    First, you need to call output.close() (or at least call output.flush()) before you reopen the file for input. That’s probably the main cause of your problems.

    Then, you shouldn’t use FileReader or FileWriter for this because it always uses the platform-default encoding (which is often not UTF-8). From the docs for FileReader:

    The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate.

    You have the same problem when using a FileWriter. Replace this:

    BufferedReader br = new BufferedReader(new FileReader("DirectionResponse.xml" ));
    

    with something like this:

    BufferedReader br = new BufferedReader(new InputStreamReader(
        new FileInputStream("DirectionResponse.xml"), "UTF-8"));
    

    and similarly for fstream.

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

Sidebar

Related Questions

Getting this error: Invalid option file options on trying to invoke Astyle via C#
Am getting this error for my code: Undefined symbols for architecture x86_64: _spendDollars, referenced
Getting this error when I submit my form to savetext.aspx action file: Compiler Error
Getting this error: 'NSInvalidArgumentException', reason: '-[EngineerModel _isNaturallyRTL]: unrecognized selector sent to instance I've read
Getting this error when I follow the simple code example from jsoncpp, that basically
Im getting this error. ReferenceError: Can't find variable: cordova at file:///android_asset/www/childbrowser.js:87 I have done
Getting this error while compiling C++ code: undefined reference to `__stack_chk_fail' Options already tried:
Im getting this error: ORA-00904: M: invalid identifier --> if I put ('M','F') //single
Im Getting this error: error: /Users/*****/Dropbox/*******/iPad:iPhone/******/*****/PNGs/../******.png: No such file or directory when I try
Getting this error Invalid custrecord_lt_partner reference key 225399. While trying to insert into The

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.