File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes save a GIF to byte , not work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "save a GIF to byte , not work" Watch "save a GIF to byte , not work" New topic

save a GIF to byte , not work

Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
Hi, all , I am trying to read a gif file to byte, and then save back to another gif file. But the new one is corrupted. Very simple, but I do not know why.
Here is the coding
public class GifToByte {
public static void main(String[] aa) throws Exception{
byte[] a;
String tem;
FileInputStream fr = new FileInputStream("Original.gif");
DataInputStream dis = new DataInputStream(fr);
a = new byte[dis.available()];
for(int i = 0; i <dis.available() ;i++){
a[i] = dis.readByte();
//tem = new String(a);
FileOutputStream frr = new FileOutputStream("xxx.gif");
DataOutputStream diss = new DataOutputStream(frr);
diss.write(a, 0, a.length);
System.out.println("------ finished --------");

Joe Ess

Joined: Oct 29, 2001
Posts: 9189

InputStream.available() doesn't give you the file size. According to the documentation:

available() Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.

so it could be pretty much any value up to the size of the file. Try File.length() if you want the size of the file. Better yet, check out the IO examples in the Java Platform Performance Guide.

[How To Ask Questions On JavaRanch]
I agree. Here's the link:
subject: save a GIF to byte , not work
It's not a secret anymore!