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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:24:42+00:00 2026-05-21T22:24:42+00:00

I am trying to write to a file certain number of bytes lets say

  • 0

I am trying to write to a file certain number of bytes lets say x bytes, with a value y. The problem is that i am doing it lots of time and I am currently using DataOutputStream with BufferedOutputStream and the write method that gets an array of byte. Each time i allocate a new array of bytes of length x and write it. It is wasteful and I wonder if there is a more efficient way?
Thanks.

Edit: I have implemented it by allocating a big array that grows by demand, but the problem is that i store it and it might get very big.
The code:

 byte[] block = new byte[4096];
    try {
        for(int i=0; i<nameOccurence.length; ++i){
            if(nameOccurence[i] >= block.length){
                int size = ((Integer.MAX_VALUE - nameOccurence[i]) <= 0.5*Integer.MAX_VALUE) ? Integer.MAX_VALUE : (nameOccurence[i] * 2);
                block = new byte[size];
            }
            if(nameOccurence[i] == 0){
                namePointer[i] = -1;//i.e. there are no vertices with this name
                continue;
            }
            namePointer[i] = byteCounter;

            ds.writeInt(nameOccurence[i]);
            ds.write(block, 0, nameOccurence[i]*2);
            ds.write(block, 0, nameOccurence[i]*2);
            byteCounter += (4*((long)nameOccurence[i]))+4;//because we wrote an integer.
        }

where ds is DataOutputStream. Please note that the array block can grow till max int if the nameOccurence[i] is large enough.

I think that the best way is to find the max number upon all i in nameOccurence and allocate array of this length. The problem is that it can get Integer.MAX_VALUE.

Maybe it will be better to run with a loop and write 1 byte each time? please note that the underlying of DataOuputStream is BufferedOutputStream.

  • 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-21T22:24:43+00:00Added an answer on May 21, 2026 at 10:24 pm

    Use

    String strFileName = "C:/FileIO/BufferedOutputStreamDemo";
    FileOutputStream fos = new FileOutputStream(strFileName);
    fos.write(yourbytes);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to write a single byte at a certain location in a file.
I'm trying to write a batch file that takes the drive letter the batch
I'm trying to write a piece of code that reads a file line by
I'm trying to write an sql function in Postgresql that will parse a file
I am trying to write a script that will parse a local file and
I'm trying to write a simple script that goes checks the number of lines
I'm trying to write a batch process that can take an MP3 file and
Right now I'm trying to write a script that will only accept certain audio
I am trying to write to an xml file in a certain structure and
I'm trying to write a function that tells me is a certain path is

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.