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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:29:22+00:00 2026-05-30T01:29:22+00:00

What is the most efficient way to output a boolean array to (and input

  • 0

What is the most efficient way to output a boolean array to (and input from) a file in Java? I was going to use a string with each character being either ‘t’ or ‘f’ and then I thought, why not take eight time less space?

NOTE

I actually have no idea which answer is the better method, I’ve just chosen Peter’s because I understand it. Thanks to both answerers!

  • 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-30T01:29:24+00:00Added an answer on May 30, 2026 at 1:29 am

    Say you have a boolean[]

    boolean[] ar = {true,false,false,true,false,true,true,true,false,true,false,false,false,true,tr‌​ue};
    

    and you want to write this to a disk, and you don’t care how its is implemented in memory.

    public static void main(String... args) throws IOException {
        boolean[] ar = {true, false, false, true, false, true, true, true, false, true, false, false, false, true, true};
    
        FileOutputStream out = new FileOutputStream("test.dat");
        writeBooleans(out, ar);
        out.close();
    
        FileInputStream in = new FileInputStream("test.dat");
        boolean[] ar2 = new boolean[ar.length]; 
        readBooleans(in, ar2);
        in.close();
    
        System.out.println(Arrays.toString(ar));
        System.out.println(Arrays.toString(ar2));
        System.out.println("The file size was "+new File("test.dat").length()+" bytes.");
    }
    
    private static void writeBooleans(OutputStream out, boolean[] ar) throws IOException {
        for (int i = 0; i < ar.length; i += 8) {
            int b = 0;
            for (int j = Math.min(i + 7, ar.length-1); j >= i; j--) {
                b = (b << 1) | (ar[j] ? 1 : 0);
            }
            out.write(b);
        }
    }
    
    private static void readBooleans(InputStream in, boolean[] ar) throws IOException {
        for (int i = 0; i < ar.length; i += 8) {
            int b = in.read();
            if (b < 0) throw new EOFException();
            for (int j = i; j < i + 8 && j < ar.length; j++) {
                ar[j] = (b & 1) != 0;
                b >>>= 1;
            }
        }
    }
    

    prints

    [true, false, false, true, false, true, true, true, false, true, false, false, false, true, true]
    [true, false, false, true, false, true, true, true, false, true, false, false, false, true, true]
    The file size was 2 bytes.
    

    but if I look at how big the file actually is

    $ ls -l test.dat
    -rw-rw-r-- 1 peter peter 2 2012-02-19 14:04 test.dat
    $ du -h test.dat 
    4.0K    test.dat
    

    It says the length is 2 bytes, but the disk space used is actually 4 KB.

    Note: About 1 minute of your time is worth about the same as 80 MB of SSD (expensive disk, more for HDD) So if you don’t think you will be saving at least 80 MB by using this, you could be wasting your time. 😉


    You can use BitSet, which can take 16x less space as each character is 16-bit.

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

Sidebar

Related Questions

What's the most efficient way to convert the output of this function from a
Whats the most efficient way of selecting total number of records from a large
What would be the most efficient way of recording to a log (.txt) from
Whats the simplest / most efficient way to log a Javascript / jQuery output?
I'm wondering what is the most efficient way to obtain autocomplete results from multiple
What's the most efficient way to concatenate strings?
What's the most efficient way to resize large images in PHP? I'm currently using
What is the most efficient way to determine how many comments a particular blog
What is the most efficient way to get the default constructor (i.e. instance constructor
What is the most efficient way of turning the list of values of 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.