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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:18:24+00:00 2026-05-26T13:18:24+00:00

In java, how to read a fixed length from the inputstream and save as

  • 0

In java, how to read a fixed length from the inputstream and save as a file?
eg. I want to read 5M from inputStream, and save as downloadFile.txt or whatever.(BUFFERSIZE=1024)

FileOutputStream fos = new FileOutputStream(downloadFile);
byte buffer [] = new byte[BUFFERSIZE];
int temp = 0;
while ((temp = inputStream.read(buffer)) != -1)
{
    fos.write(buffer, 0, temp);
}
  • 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-26T13:18:25+00:00Added an answer on May 26, 2026 at 1:18 pm

    Two options:

    1. Just keep reading and writing until you either reach the end of the input or you’ve copied enough:

      byte[] buffer = new byte[1024];
      int bytesLeft = 5 * 1024 * 1024; // Or whatever
      FileInputStream fis = new FileInputStream(input);
      try {
        FileOutputStream fos = new FileOutputStream(output);
        try {
          while (bytesLeft > 0) {
            int read = fis.read(buffer, 0, Math.min(bytesLeft, buffer.length);
            if (read == -1) {
              throw new EOFException("Unexpected end of data");
            }
            fos.write(buffer, 0, read);
            bytesLeft -= read;
          }
        } finally {
          fos.close(); // Or use Guava's Closeables.closeQuietly,
                       // or try-with-resources in Java 7
        }
      } finally {
        fis.close(); 
      }
      
    2. Read all 5M into memory in one call, e.g. using DataInputStream.readFully, and then write it out in one go. Simpler, but obviously uses more memory.

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

Sidebar

Related Questions

How do I read multiple lines (in Java) from an input file (say, helloworld.in)?
In Java you can read a file embedded inside a JAR-file by using the
In java i would read the whole file in to byte array and do
Description | A Java program to read a text file and print each of
From what I read about Java NIO and non-blocking [Server]SocketChannels, it should be possible
I'm writing a program in Java where I read in data from an XML
I have written a program in Java to read in a text file of
I have to read a binary file in a legacy format with Java. In
What I am trying to do is read from a text file where each
I am programming a simple proxy in java: Read XML File Send request to

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.