How to write bytes inside a file in Java ?

T

tobleron

Hi,

Currently I'm starting to write a DICOM file in Java. I've wrote it
before in C, and I used fwrite() command to write byte by byte. What
is the equal command as fwrite() in Java ?

Best regards.
 
L

Lothar Kimmeringer

tobleron said:
Currently I'm starting to write a DICOM file in Java. I've wrote it
before in C, and I used fwrite() command to write byte by byte. What
is the equal command as fwrite() in Java ?

In Java Streams are used instead of the use of (file-)handles
as it is the case in C AFAIR.

java.io_OutputStream is already working with byte, so if you
have a ready-to-write block of bytes you can simply do the
following:

FileOutputStream fos = new FileOutputStream(filename);
fos.write(myDataBloc);
fos.close();

You can also write element by element e.g. with DataOutputStream,
but you have to keep in mind that DataOutputStream.writeInt always
writes an int in HSB-order independent from the underlying OS.
So if DICOM is working with LSB-order (I don't know and I will not
check now ;-) you have to create your own byte-array from an int
instead of using that helper-method.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
N

Nigel Wade

tobleron said:
Hi,

Currently I'm starting to write a DICOM file in Java. I've wrote it
before in C, and I used fwrite() command to write byte by byte. What
is the equal command as fwrite() in Java ?

The most fundamental file output class is probably FileOutputStream. This has
the overloaded method write() which can write a byte array, part of a byte
array or a single byte. The method most closely resembling fwrite() is
FileOutputStream.write(byte[] b, int off, int len). To use this method you
create an instance of a FileOutputStream and then invoke the write() method on
that object.

If you want to read up on the basics of Java I/O then the Java Tutorial on the
Essential Classes has this section on I/O and the fundamental classes:
http://java.sun.com/docs/books/tutorial/essential/io/index.html
 
T

tobleron

Hi.... how to write HEX data into the stream ?
For example I have data like this :

data = 0x0000008c;

I want to write this hex data into the stream, how to do it ? I tried
to use .write() .writeByte() .writeShort() but can't works. Please
advise. Thx.
 
J

John B. Matthews

tobleron said:
Hi.... how to write HEX data into the stream ?
For example I have data like this :

data = 0x0000008c;

I want to write this hex data into the stream, how to do it ? I tried
to use .write() .writeByte() .writeShort() but can't works. Please
advise. Thx.

Using DataOutputStream, writeByte(0x0000008c) writes one byte, while
writeInt(0x0000008c) writes four bytes in big-endian order. Which did
you intend?

import java.io.*;
public class WriteIntTest {
public static void main(String[] args) throws IOException {
DataOutputStream out =
new DataOutputStream (
new FileOutputStream("tempB.bin"));
out.writeInt(0x0000008c);
out.close();
}
}

$ hd tempB.bin
000000: 00 00 00 8c
 
J

John B. Matthews

Lew said:
John said:
tobleron said:
Hi.... how to write HEX data into the stream ?
For example I have data like this :

data = 0x0000008c;

I want to write this hex data into the stream, how to do it ? I tried
to use .write() .writeByte() .writeShort() but can't works. Please
advise. Thx.

Using DataOutputStream, writeByte(0x0000008c) writes one byte, while
writeInt(0x0000008c) writes four bytes in big-endian order. Which did
you intend?

import java.io.*;
public class WriteIntTest {
public static void main(String[] args) throws IOException {
DataOutputStream out =
new DataOutputStream (
new FileOutputStream("tempB.bin"));
out.writeInt(0x0000008c);
out.close();
}
}

$ hd tempB.bin
000000: 00 00 00 8c

Bearing in mind, OP, that the data isn't actually stored in "hex" - hex is a
string representation of a binary value.

OP: This is a subtle but important point. Above, I used the command
"hd" to display the output-file's binary contents in hexadecimal. For
reference, "hd" is a short script that formats the output of the
hexdump command, which is common to Linux and BSD Unix:

#!/usr/bin/hexdump -f
"%06.6_ax: " 16/1 "%02x " " " 16/1 "%_p" "\n"
 
R

Roedy Green

Currently I'm starting to write a DICOM file in Java. I've wrote it
before in C, and I used fwrite() command to write byte by byte. What
is the equal command as fwrite() in Java ?

see http://mindprod.com/applet/fileio.html
for example code to do various tasks.

If you want to just change a few bytes in the middle of a file, you
might want to use Random file access. It is more for bytes or binary
data than chars since strings are variable length when encoded.
 
T

tobleron

@All,
Thank you for your suggestions.

@John
I intend in little endian order. I'll try your suggestions. Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top