how to convert an image file to binary string in java

Would like to convert the image such as "forward.gif" into a binary string in java.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


The class on that page also has a method to convert a File to byte[]. You can then Base64-encode that array
jl66Author Commented:
CEHJ, thanks a lot for the tip.
Could you please explain it a bit detailed to me? I guess the method "copyStream" I should use. Right?
IOUtils.inputStreamToByteArray would be the method you'd need

However, since Java (shame on it) doesn't have a supported Base64 class, you might as well use Apache Commons which will give you a Base64 class and the io routines[])
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

jl66Author Commented:
Thanks a lot for more tips. It took a while to make all work.
However, what was got is
"[71, 73, 70, 56, 57, 97, 32, 0, 32, 0, -9,......]" after calling
FileInputStream imageInFile = new FileInputStream(file);
byte imageData[] = new byte[(int) file.length()];;
and then calling
got the following string
"R0lGODlhIAAgAPcAAP_9weHSof7xgP7......". However, what is needed is a binary string that the low level hardware can recognize. A basic conversion is needed here. It does not seem necessary to encode/decode it.
It does not seem necessary to encode/decode it.
It is necessary actually. What is the alternative? It's not a string of characters. You could represent it as a string of hex numbers but

a. it would be longer than Base64
b. it's still encoded
jl66Author Commented:
After reading the image file to byte array, we analyzing the number series: 71, 73, 70, 56, 57, 97, 32, 0, 32, 0, -9,..... Why does each number stands for? Does it represent each byte for the image file? How about "-9"?
These are not the ACSII codes. What codes are they? It seems to have more questions.
These are not the ACSII codes. What codes are they?
No, they are not ASCII codes. Such codes are for representing text. You have an image and those are its raw bytes. The byte type is signed in  Java, so the maximum value is 127 and thereafter it starts going negative

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jl66Author Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.