Results 1 to 13 of 13
Thread: FileOutputStream to GIF
- 10-09-2010, 07:36 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 67
- Rep Power
- 0
FileOutputStream to GIF
Hey there
I am confused, and have a lack of understanding about what FileOutputStream actually does.
Heres my code I got from a book:
I then open "pic.gif" in notepad and it gives me:Java Code:package test; import java.io.*; public class ByteWriter { /** * @param args */ public static void main(String[] args) { int[] data = {71, 73, 70, 56, 57, 97, 13, 0, 12, 0, 145, 0, 0, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 44, 0, 0, 0, 0, 13, 0, 12, 0, 0, 2, 38, 132, 45, 121, 11, 25, 175, 150, 120, 20, 162, 132, 51, 110, 106, 239, 22, 8, 160, 56, 137, 96, 72, 77, 33, 130, 86, 37, 219, 182, 230, 137, 89, 82, 181, 50, 220, 103, 20, 0, 59}; try{ FileOutputStream file = new FileOutputStream("pic.gif"); for(int i = 0; i<data.length; i++) file.write(data[i]); file.close(); }catch(IOException e){ System.out.println("Error -- " + e.toString()); } } }
How did the array get "translated" to the content inside the gif?Java Code:GIF89a ‘ ÿÿÿÿÿ , &„-y¯–x¢„3njï*8‰`HM!‚V%Û¶æ‰YRµ2Üg ;
Sorry if this is a stupid question
Thanks
- 10-09-2010, 07:43 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
It isn't; you didn't write a text file and notepad can only properly show text files. A quick check shows that you have correctly written all the bytes in the original array. For a more advanced class have a look at the ImageIO class; it can read and write pictures (gif, jpeg, png etc) in several formats.
kind regards,
Jos
- 10-09-2010, 07:55 AM #3
Member
- Join Date
- Aug 2010
- Posts
- 67
- Rep Power
- 0
Thanks for the reply.
I want to know what the method "write" actually does.
What does it actually do to the input data from the array exactly?
thanks
- 10-09-2010, 08:07 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
What does it actually do to the input data from the array exactly?
The for loop works along the array and, for each int value it finds, the write() method "Writes the specified byte to this file output stream. (Implements the write method of OutputStream)", that is, it "Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored."
As always a description of what a method does is to be found in its API documentation.
Saying that each byte is written to the output stream (the file) is about all you can say, along with the observation that the bytes will be exactly the same as those you later obtain by reading them from the file using a FileInputStream.
- 10-09-2010, 08:16 AM #5
Member
- Join Date
- Aug 2010
- Posts
- 67
- Rep Power
- 0
Thanks for the quick reply.
I don't quite get what is meant by the terms "eight low-order bits" and "high-order bits".
Does the write method translate the input to the binary code equivalent? or something?
Thanks for your time.
- 10-09-2010, 08:56 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
I don't quite get what is meant by the terms "eight low-order bits" and "high-order bits".
Does the write method translate the input to the binary code equivalent?
Let's look at the very first int in the array: 72.
Now, strictly speaking, 72 is just a number. Various representations of it may involve bits or have a base like binary or decimal. But the number is just a number: six dozen in this case.
However it's convenient to think of the number in its binary form:
Java Code:00000000000000000000000001001000
I could have written it in binary as 1001000 but I put all those zeros on the left because Java ints can typically get that big. The left hand end of the number is called the "high order bits" and the right hand end the "low order bits". I hope the reason is obvious. (say if not)
Now the documentation is saying that when write() comes to write an int only the 8 low order (right hand) bits are written. That is only 01001000. The other 24 bits are thrown away. Consequently the following ints (whatever they are!) will all be written the same as 72:
Java Code:00000000000000000000000001001000 01010101010101010101010101001000 11111111111111111111111101001000
If you think about it for a while you will see that there are exactly 256 different values that can be written this way: from 00000000 (== zero) to 11111111 (== CCLV) which are referred to as bytes. (I write them with words or roman numerals to stress the fact that as numeric quantities they have neither bits nor base - important as bits might be to the folk who make disk drives)
You might be wondering why the write method doesn't take a byte as an argument rather than an int if it only going to ignore those 24 bits on the left. I would guess this is related to the fact that Java's language designers decided to use "byte" for a different set of 256 values: those from -128 to 127.Last edited by pbrockway2; 10-09-2010 at 09:01 AM.
- 10-09-2010, 11:55 AM #7
Member
- Join Date
- Aug 2010
- Posts
- 67
- Rep Power
- 0
Thank you
That cleared allot up.
So just to clarify
I ran this bit of code:
I opened pic.gif in notepad, and it gave me:Java Code:package test; import java.io.*; public class ByteWriter { /** * @param args */ public static void main(String[] args) { int[] data = {72}; try{ FileOutputStream file = new FileOutputStream("pic.gif"); for(int i = 0; i<data.length; i++) file.write(data[i]); file.close(); }catch(IOException e){ System.out.println("Error -- " + e.toString()); } } }
So the write method converted 72 in to the Ascii character equivalent?Java Code:H
- 10-09-2010, 12:07 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 10-09-2010, 03:13 PM #9
Member
- Join Date
- Aug 2010
- Posts
- 67
- Rep Power
- 0
oh ok thank you for your time mate
- 10-10-2010, 02:10 AM #10
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
why are you using notepad to open a .gif file ???
- 10-10-2010, 07:03 AM #11
Member
- Join Date
- Aug 2010
- Posts
- 67
- Rep Power
- 0
- 10-10-2010, 01:32 PM #12
Notepad only can show character data. The binary bytes that can be in gif files will show as ? or as small boxes. You need to use a hex editor to look at binary files like gifs.To see exactly what is in the file
- 10-11-2010, 07:12 AM #13
Member
- Join Date
- Aug 2010
- Posts
- 67
- Rep Power
- 0
Similar Threads
-
closing FileOutputStream?
By mgrootsch in forum New To JavaReplies: 1Last Post: 05-17-2010, 05:36 PM -
FileOutputStream > int > FileInputStream
By dudejonne in forum New To JavaReplies: 11Last Post: 11-11-2009, 04:03 PM -
FileOutputStream, I dont know what Im doing wrong...
By Addez in forum New To JavaReplies: 6Last Post: 09-14-2009, 10:46 PM -
FileOutputStream gets NotSerializableException
By xcallmejudasx in forum New To JavaReplies: 0Last Post: 12-02-2008, 09:38 PM -
FileOutputStream question...
By SCS17 in forum New To JavaReplies: 2Last Post: 07-07-2008, 05:30 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks