File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

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
Thanks.
Edward
-------------
import java.io.*;
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);
//System.out.println(tem);
dis.close();
fr.close();
FileOutputStream frr = new FileOutputStream("xxx.gif");
DataOutputStream diss = new DataOutputStream(frr);
diss.write(a, 0, a.length);
diss.flush();
diss.close();
frr.close();
System.out.println("------ finished --------");

}
}
----------------
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8711
    
    6

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: save a GIF to byte , not work
 
Similar Threads
Why picture is not being copy
DataInputStream Help needed while creating file
how to read line by line and pass each line value to a method
problem in reading and writing a file
Split and Merge large zip files