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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:57:37+00:00 2026-05-25T18:57:37+00:00

It must be obvious, but I can’t figure it out. I spent almost a

  • 0

It must be obvious, but I can’t figure it out.
I spent almost a whole day for this.
I’ll gladly buy a beer to someone who can lighten me.

File file = new File(filePath);
byte[] bytes = new byte[(int)file.length()];
DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath)));
dataInputStream.readFully(bytes);           
dataInputStream.close();            
return new String(bytes);

This is my code.
I see that the byte array size is not OK, but I can’t figure out the right size.
Besides that the contents are not also incorrect.
It seems that only the text characters are OK.

It seems get out the data from a binary file is a real pain, I’m really depressed.

One more thing: the file contents are not text, it can be anything like a picture, video, or pdf.

  • 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-05-25T18:57:38+00:00Added an answer on May 25, 2026 at 6:57 pm

    If you’re reading a binary file you should not try to treat it as if it were encoded text. It’s inappropriate to convert it to a string like this – you should keep it as a byte array. If you really need it as text, you should use base64 or hex to represent the binary data – other approaches are likely to lose data.

    If readFully returned without an exception, that shows it’s read as much data as you requested, which should be the whole file. You’ve managed to get the data from a binary file fairly easily (although the close() call should be in a finally block) – it’s only converting it to text which is a bad idea.

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

Sidebar

Related Questions

This question must be obvious but I can't figure it out. In a template,
I must be doing something obvious, but I can't figure out what it is.
I thought that must be obvious but I can't figure it out. Say there
I must be missing something obvious, but I can't figure out how to rename
This must be obvious, but I have searched for ages and can find no
This seems so stupidly simple, but I can't figure it out. I'd like to
This must be something obvious, but I can't get this to work. I'm trying
It's probably obvious to you, but I can't figure it out. I need to
I must be missing something extremely obvious but I can't get this work properly.
I must be missing something really obvious here, but I can't seem to figure

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.