Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to read file into n-bit array

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to read file into n-bit array

    Hi,

    Is there any way to read a binary file into n-bit array?
    There is a file that I want read which consists of 10-bit unsigned integers
    I want to read them into an int arrray. Is there any way for this?
    Thanx


  2. #2
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: How to read file into n-bit array

    There's not 'bit' datatype in Java. Hence, if you really want to use bits, you'll have to use bytes or booleans or chars or String or something (though booleans' size is 1 byte (or even bigger, I've heard once that it depends on the JVM implementation...)).
    To do what you want you can use the following algorithm:
    1. Read all the bytes from the file into an array
    2. Define a function that would take a byte and return it's bits in a chosen format. Btw, consider Integer.toBinaryString(), which takes an integer and returns a string with it's bits.
    3. Apply the (2) function to (1) array in order to represent bits of the file in memory
    4. Parse (3) datastructure in a way that corresponds your needs

  3. #3
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: How to read file into n-bit array

    i dont know if its just me but i have found it hard to work with unsigned integers in java

  4. #4
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: How to read file into n-bit array

    Is it so difficult, for example, to create a separate class to deal with that data type?

Similar Threads

  1. [SOLVED] Why is my array not being read in correctly from my file?
    By gabie1121 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 14th, 2013, 10:55 AM
  2. How to read a String .dat file into an array
    By Wallatrix in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: March 5th, 2013, 02:55 PM
  3. How to read a 2 dimensional text file from an array?
    By seaofFire in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 9th, 2012, 07:44 AM
  4. Read txt file into array and create new output.
    By Margaret_Girl87 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 19th, 2012, 12:08 PM
  5. how to read a text delimited file using 2 dimentional array in java ??
    By pooja123 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 1st, 2011, 09:11 AM