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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:46:28+00:00 2026-06-08T06:46:28+00:00

How can I write/read a string from a binary file? I’ve tried using writeUTF

  • 0

How can I write/read a string from a binary file?

I’ve tried using writeUTF / readUTF (DataOutputStream/DataInputStream) but it was too much of a hassle.

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-08T06:46:30+00:00Added an answer on June 8, 2026 at 6:46 am

    Forget about FileWriter, DataOutputStream for a moment.

    • For binary data one uses OutputStream and InputStream classes. They handle byte[].
    • For text data one uses Reader and Writer classes. They handle String which can store all kind of text, as it internally uses Unicode.

    The crossover from text to binary data can be done by specifying the encoding, which defaults to the OS encoding.

    • new OutputStreamWriter(outputStream, encoding)
    • string.getBytes(encoding)

    So if you want to avoid byte[] and use String you must abuse an encoding which covers all 256 byte values in any order. So no “UTF-8”, but maybe “windows-1252” (also named “Cp1252”).

    But internally there is a conversion, and in very rare cases problems might happen. For instance é can in Unicode be one code, or two, e + combining diacritical mark right-accent '. There exists a conversion function (java.text.Normalizer) for that.

    One case where this already led to problems is file names in different operating systems; MacOS has another Unicode normalisation than Windows, and hence in version control system need special attention.

    So on principle it is better to use the more cumbersome byte arrays, or ByteArrayInputStream, or java.nio buffers. Mind also that String chars are 16 bit.

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

Sidebar

Related Questions

How can I read and write comments in a Zip file, using Java? I
How can i check the read/ write permission of the file storing media? ie
Can we write files to the file system and leave them as read-only in
Can anybody give any working example of how to read/write Unicode text files using
I can easily write a string to a connection using io.WriteString. However, I can't
I want to delete a char/string from a binary/text file. If I know the
How can I write binary number in code without using Convert.ToInt16(00001000, 2); I want
I would like to write/read a hash-table to/from disk, but it is not a
I'm trying to read a remote binary file (say, image) from internet like this:
I want to write matching expression to read string between parentheses () from a

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.