Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Writing Binaries to and Reading Binaries from Disk

Reply
Thread Tools

Writing Binaries to and Reading Binaries from Disk

 
 
KevinSimonson
Guest
Posts: n/a
 
      09-26-2009
I'm planning on writing a program that stores a lot of binary informa-
tion, and I'd like to be able to write it to disk when I'm done with
it, so that later I can read it in from disk again. How do you do
that in Java, write binary <short>s to disk and then later read in
those <short>s from disk again?

Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      09-26-2009
KevinSimonson wrote:
> I'm planning on writing a program that stores a lot of binary informa-
> tion, and I'd like to be able to write it to disk when I'm done with
> it, so that later I can read it in from disk again. How do you do
> that in Java, write binary <short>s to disk and then later read in
> those <short>s from disk again?
>
> Kevin Simonson
>
> "You'll never get to heaven, or even to LA,
> if you don't believe there's a way."
> from _Why Not_


I'm not sure what a binary <short> is but to write a Java short to a
stream you can use the DataOutputStream class. Also you can convert it
to bytes and use a regular OutputStream. Also see BufferedOutputStream.

import java.io.*;

public class test4 {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("data.dat");
DataOutputStream dos = new DataOutputStream(fos);
short s1 = -32768;
dos.writeShort(s1);
dos.close();

FileInputStream fis = new FileInputStream("data.dat");
DataInputStream dis = new DataInputStream(fis);
short s2 = dis.readShort();
dis.close();

System.out.print(s2);
}
}

--

Knute Johnson
email s/nospam/knute2009/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
 
Reply With Quote
 
 
 
 
Lothar Kimmeringer
Guest
Posts: n/a
 
      09-26-2009
KevinSimonson wrote:

> I'm planning on writing a program that stores a lot of binary informa-
> tion, and I'd like to be able to write it to disk when I'm done with
> it, so that later I can read it in from disk again. How do you do
> that in Java, write binary <short>s to disk and then later read in
> those <short>s from disk again?


IO-operations are byte-oriented (this is not java specific)
so when writing shorts (2 bytes) there are two ways to do
this and it depends if the data you write to disk should
be read by other programs as well. So before you use
DataOutputStream you should check if the format to be used
is LSB least significant byte first or HSB (high significant
byte first). HSB is used with DataOutputStream, if you
want to write LSB you need to do the writing for yourself:

outstream.write(myshort & 0xff);
outstream.write((myshort >> & 0xff);

other direction:

short myshort = instream.read() & 0xff;
myshort |= (instream.read() << & 0xff;


Regards, Lothar
--
Lothar Kimmeringer E-Mail: http://www.velocityreviews.com/forums/(E-Mail Removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      09-26-2009
Lothar Kimmeringer wrote:
> KevinSimonson wrote:
>
>> I'm planning on writing a program that stores a lot of binary informa-
>> tion, and I'd like to be able to write it to disk when I'm done with
>> it, so that later I can read it in from disk again. How do you do
>> that in Java, write binary <short>s to disk and then later read in
>> those <short>s from disk again?

>
> IO-operations are byte-oriented (this is not java specific)
> so when writing shorts (2 bytes) there are two ways to do
> this and it depends if the data you write to disk should
> be read by other programs as well. So before you use
> DataOutputStream you should check if the format to be used
> is LSB least significant byte first or HSB (high significant
> byte first). HSB is used with DataOutputStream, if you
> want to write LSB you need to do the writing for yourself:
>
> outstream.write(myshort & 0xff);
> outstream.write((myshort >> & 0xff);
>
> other direction:
>
> short myshort = instream.read() & 0xff;
> myshort |= (instream.read() << & 0xff;


Three of the 0xff masks are superfluous and the fourth
is incorrect (assuming "outstream" is a java.io.OutputStream
and "instream" a java.io.InputStream). Instead, try

outstream.write(myshort);
outstream.write(myshort >> ;

and

short myshort = instream.read();
myshort |= instream.read() << 8;

or even

short myshort = instream.read() | (instream.read() << ;

.... although as a dyed-in-the-wool C programmer this last version
makes me cringe.

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
Lothar Kimmeringer
Guest
Posts: n/a
 
      09-26-2009
Patricia Shanahan wrote:

> Lothar Kimmeringer wrote:
>>
>> outstream.write(myshort & 0xff);
>> outstream.write((myshort >> & 0xff);
>>
>> other direction:
>>
>> short myshort = instream.read() & 0xff;
>> myshort |= (instream.read() << & 0xff;

>
> What advantages does this have compared to wrapping the output stream in
> a DataOutputStream and using its writeShort method?


As I said (and you quoted it above):
>> HSB is used with DataOutputStream, if you
>> want to write LSB you need to do the writing for yourself:



Regards, Lothar
--
Lothar Kimmeringer E-Mail: (E-Mail Removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
Reply With Quote
 
Lothar Kimmeringer
Guest
Posts: n/a
 
      09-26-2009
Eric Sosman wrote:

> Lothar Kimmeringer wrote:
>> outstream.write(myshort & 0xff);
>> outstream.write((myshort >> & 0xff);
>>

> Three of the 0xff masks are superfluous


I know but it's a way of telling other that I really
want to write only the bits 0 to 7.

>> other direction:
>>
>> short myshort = instream.read() & 0xff;
>> myshort |= (instream.read() << & 0xff;

> and the fourth
> is incorrect


True. Slipped my mind.

> (assuming "outstream" is a java.io.OutputStream
> and "instream" a java.io.InputStream). Instead, try
>
> outstream.write(myshort);
> outstream.write(myshort >> ;


As I said, the & 0xff is my way of documenting and to make
sure that I don't forget the parts where it's used.

> and
>
> short myshort = instream.read();
> myshort |= instream.read() << 8;
>
> or even
>
> short myshort = instream.read() | (instream.read() << ;
>
> ... although as a dyed-in-the-wool C programmer this last version
> makes me cringe.


Of course it should always be checked if the end of stream
hasn't been reached


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (E-Mail Removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
Reply With Quote
 
KevinSimonson
Guest
Posts: n/a
 
      09-26-2009
On Sep 26, 1:45*pm, Knute Johnson <(E-Mail Removed)>
wrote:

=I'm not sure what a binary <short> is but to write a Java short to a
=stream you can use the DataOutputStream class. Also you can convert
=it to bytes and use a regular OutputStream. Also see
=BufferedOutputStream.

Thanks for the example snippet of code; I'm going to start using it
right away. But when I looked in the API for the description of
<BufferedOutputStream> and <BufferedInputStream>, I couldn't figure
out exactly how I'd use those classes to write Java <short>s to disk
and read them from disk.

I guess I could convert each <short> to a <byte> array and then call
<write( toDiskArray, 0, 2)>, and then call
<read( fromDiskArray, 0, 2)>, and then convert <fromDiskArray> back
into a <short>, but this just seems like a really messy way to write
and read a <short>. Is there a better way?

Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_
 
Reply With Quote
 
Lothar Kimmeringer
Guest
Posts: n/a
 
      09-26-2009
Patricia Shanahan wrote:

>>>> HSB is used with DataOutputStream, if you
>>>> want to write LSB you need to do the writing for yourself:

>
> Remember the original question "How do you do that in Java, write binary
> <short>s to disk and then later read in those <short>s from disk again?"
>
> What advantage to you see, given that requirement, to writing the data
> in little-endian format?


You don't like reading, do you? I also wrote

>>>> So before you use
>>>> DataOutputStream you should check if the format to be used
>>>> is LSB least significant byte first or HSB (high significant
>>>> byte first).


The OP didn't say anything about other programs but there is nothing
wrong in checking before if that is the case before you use a
specific encoding and regret it later.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (E-Mail Removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
Reply With Quote
 
Lothar Kimmeringer
Guest
Posts: n/a
 
      09-26-2009
KevinSimonson wrote:

> Thanks for the example snippet of code; I'm going to start using it
> right away. But when I looked in the API for the description of
> <BufferedOutputStream> and <BufferedInputStream>, I couldn't figure
> out exactly how I'd use those classes to write Java <short>s to disk
> and read them from disk.
>
> I guess I could convert each <short> to a <byte> array and then call
> <write( toDiskArray, 0, 2)>, and then call
> <read( fromDiskArray, 0, 2)>, and then convert <fromDiskArray> back
> into a <short>, but this just seems like a really messy way to write
> and read a <short>. Is there a better way?


I'm sure he meant it that way:

FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
DataOutputStream dos = new DataOutputStream(bos);

Then you can still use writeShort without writing every single
directly to the disk which would lead to a drop in performance.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (E-Mail Removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
Reply With Quote
 
Qu0ll
Guest
Posts: n/a
 
      09-26-2009
"KevinSimonson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I'm planning on writing a program that stores a lot of binary informa-
> tion, and I'd like to be able to write it to disk when I'm done with
> it, so that later I can read it in from disk again. How do you do
> that in Java, write binary <short>s to disk and then later read in
> those <short>s from disk again?


The advice so far has been to use traditional java.io but I have found that
you'll get better performance if you use nio and especially if you use a
MappedByteBuffer which has methods to put shorts, ints etc.

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
(E-Mail Removed)
[Replace the "SixFour" with numbers to email me]

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing a <char> Array to Disk and Reading it in again KevinSimonson C++ 12 10-20-2010 09:45 PM
Reading and Writing <byte> and <char> Values from and to Disk Quickly kvnsmnsn@hotmail.com Java 7 07-06-2006 04:21 AM
Scan Disk and Disk Defragmenter ASAP Palmira Vicks Computer Support 2 01-01-2004 07:59 AM
reading from a file without actually writing into its hard disk copy Jeevan C Programming 4 10-21-2003 07:17 AM



Advertisments