Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

save a GIF to byte , not work

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9258
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic