(Java) Write decimal/hex to a file, and not string?

"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!

For writing binary data you'll want to use a OutputStream (such as a FileOutputStream ).

Up vote 2 down vote favorite share g+ share fb share tw.

I want to be able to use something like outfile. Write(42) or outfile. Write(2a) and not write the string to the file.

(I realize this is a simple question but I can't find the answer of google, probably because I don't know the correct search terms) java string decimal write hex link|improve this question edited Sep 7 '09 at 9:18 asked Sep 7 '09 at 9:15NMoney1,82731744 92% accept rate.

For writing binary data you'll want to use a OutputStream (such as a FileOutputStream). If you find that your data is written as strings, then you're probably using a Writer (such as a FileWriter or a OutputStreamWriter wrapped around a FileOutputStream). Everything named "*Writer" or "*Reader" deals exclusively with text/Strings.

You'll want to avoid those if you want to write binary data. If you want to write different data types (and not just plain bytes), then you'll want to look into the DataOutputStream.

Ah! Just late by a second. Good one!

:) – Aviator Sep 7 '09 at 9:18 just as an interesting aside: have a look at the java.util.zip. * classes that provide on-the-fly compression. You can pass them to the DataOutputStream constructor and save quite a lot of disk IO – Otto Allmendinger Sep 7 '09 at 9:32.

There's no need to use DataOutputStream here. – Jon Skeet Sep 7 '09 at 9:19 Could you explain me why? Anyway, I just said that he could use it.

:) – Aviator Sep 7 '09 at 9:21 If you want to write bytes, then any OutputStream is ok. Only if you want to write ints/longs/floats/doubles/... directly, then you can use a DataOutputStream to do the conversion to bytes for you. – Joachim Sauer Sep 7 '09 at 9:26 Oh!

Okie.. Got it.. thanks! – Aviator Sep 7 '09 at 9:27 I took the liberty of updating the link to point to a current version of the docs. There are too many links to ancient documentation out there ;-) – Joachim Sauer Sep 7 '09 at 9:33.

If you just want to write bytes, the following will suffice: import java.io. *; ... OutputStream out = new FileOutputStream("MyFile"); try { // Write one byte ... out. Write((byte) 42); // Write multiple bytes ... byte bytes = ... int nosWritten = out.

Write(bytes, 0, bytes. Length); } finally { out.close(); } Exception handling is left as an exercise for the reader :-).

Actually if you want to write the whole array then "out. Write(bytes)" will be ok, you don't need to use the 3-arguments version. – Joachim Sauer Sep 7 '09 at 9:28 @Joachim: I know.

It is an example. – Stephen C Sep 7 '09 at 13:00.

OutputStream os = new FileOutputStream(fileName); String text = "42"; byte value = Byte. ParseByte(text); os. Write(value); os.close().

Related Questions