• Post Reply Bookmark Topic Watch Topic
  • New Topic

Storing images in a file  RSS feed

 
nadia mcday
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello,

I am writing a program that receives an image from a server and stores the bytes of this images in a buffered stream. How can I then store this image in a file in a specific directory?
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nadia mcday wrote:
How can I then store this image in a file in a specific directory?


Are you comfortable with Java IO ?

Have you looked at Image IO API from SUN.
 
nadia mcday
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am not too comfortable with IO, to tell the truth! I looked at the image IO class, it seems to be what I need, but if I have a stream of bytes how can I convert it to a buffered image? In fact what I am doing is reading the data from a socket. Is there a way to read the a buffered image directly from the socket?
Thanks a lot!
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have the raw bytes to work with, then creating a BufferedImage first, just so that you can then use ImageIO, is probably not the best route.

The java.io.File, java.io.FileOutputStream, and (optionally) java.io.BufferedOutputStream classes should be all you need.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sagar Rohankar wrote:
nadia mcday wrote:
How can I then store this image in a file in a specific directory?


Are you comfortable with Java IO ?

Have you looked at Image IO API from SUN.

To expand what Ulf mentioned: the Java IO API is not to be confused with the Java 2D API. Those are completely unrelated to each other.

The Java IO API is documented here: http://java.sun.com/javase/6/docs/api/java/io/package-summary.html
There is a tutorial as well: http://java.sun.com/docs/books/tutorial/essential/io/
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!