How do I write an ImageIcon object into a file on my computer?

Discussion in 'Java' started by phillip.s.powell@gmail.com, Jan 16, 2007.

  1. Guest

    I am about to throw in the towel on Java at this point! Too many
    questions!

    How in the world do you do this one (again, in my native PHP it's
    extremely easy:

    $fileID = @fopen('/path/to/your/new/file', 'wb');
    if ($fileID) {
    @fputs($fileID,
    file_get_contents('http://www.domain.com/someimage.jpg'));
    @fclose($fileID);
    }

    That's it. Now, how on earth do I do that in Java???

    I tried something like this but just fell apart on it:

    /*
    * FileDownloader.java
    *
    * Created on January 10, 2007, 3:47 PM
    *
    * To change this template, choose Tools | Template Manager
    * and open the template in the editor.
    */

    package FileTools;

    import java.io.*;
    import java.net.*;


    /**
    *
    * @author ppowell-c
    */
    public class FileDownloader implements Serializable {

    /** Creates a new instance of FileDownloader */
    public FileDownloader() {}

    public static void download(URL url, File file) throws IOException
    {
    InputStream in = url.openStream();
    FileOutputStream out = new FileOutputStream(file);
    byte[] b = new byte[1024];
    int len;
    while((len = in.read(b)) != -1) {
    out.write(b, 0, len);
    }
    out.close();
    }

    public static void toFile(File file, Object contents) throws
    IOException {
    FileOutputStream out = new FileOutputStream(file);
    out.write(contents.getBytes());
    }
    }

    This of course fails, but I have absolutely no idea how to get the
    ImageIcon object "contents" to write into the file "/path/to/my/file".

    Phil
    , Jan 16, 2007
    #1
    1. Advertising

  2. Daniel Pitts Guest

    wrote:
    > I am about to throw in the towel on Java at this point! Too many
    > questions!
    >
    > How in the world do you do this one (again, in my native PHP it's
    > extremely easy:
    >
    > $fileID = @fopen('/path/to/your/new/file', 'wb');
    > if ($fileID) {
    > @fputs($fileID,
    > file_get_contents('http://www.domain.com/someimage.jpg'));
    > @fclose($fileID);
    > }
    >

    [snip]
    > public static void download(URL url, File file) throws IOException
    > {
    > InputStream in = url.openStream();
    > FileOutputStream out = new FileOutputStream(file);
    > byte[] b = new byte[1024];
    > int len;
    > while((len = in.read(b)) != -1) {
    > out.write(b, 0, len);
    > }
    > out.close();
    > }
    >
    > public static void toFile(File file, Object contents) throws
    > IOException {
    > FileOutputStream out = new FileOutputStream(file);
    > out.write(contents.getBytes());
    > }
    > }
    >
    > This of course fails, but I have absolutely no idea how to get the
    > ImageIcon object "contents" to write into the file "/path/to/my/file".
    >
    > Phil

    Hmm, are you trying to save an image (such as a jpeg or gif), or
    actually trying to serialize the ImageIcon object to a file? It looks
    like you are simply trying do download data from a URL (thats what your
    PHP code does). your toFile method can not do that if you have just any
    Object. Only certain ones allow you to get at the byte[] contents. If

    What doesn't work about your download method?
    Other than the fact that you forget call "in.close()" in a failsafe
    manor (see below)
    If that is all you really want to do, then it is the best way.
    However, if you have an Image object, look into ImageIO:
    <http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageIO.html> It
    might do what you want.


    -- This is a safer way to save a binary file from a URL:

    public static void download(URL url, File file) throws IOException {
    final InputStream in = url.openStream();
    try {
    final OutputStream out = new FileOutputStream(file);
    try {
    final byte[] b = new byte[1024];
    int len;
    while((len = in.read(b)) != -1) {
    out.write(b, 0, len);
    }
    } finally {
    out.close();
    }
    } finally {
    in.close();
    }
    }
    Daniel Pitts, Jan 16, 2007
    #2
    1. Advertising

  3. Guest

    I hope this makes it more clear:

    *update* still fails, can't fix it this time..

    Here are my methods

    public static void toFile(File file, Object contents) throws
    IOException {
    FileOutputStream out = new FileOutputStream(file);
    out.write(contents.toString().getBytes());
    }

    public static void toFile(String filePath, Object contents) throws
    IOException {
    File file = new File(filePath);
    toFile(file, contents);
    }
    }

    I get this exception thrown:

    file:\C:\Documents%20and%20Settings\me\stave.ico (The filename,
    directory name, or volume label syntax is incorrect)

    I have no idea what to do. All I want to do is create an icon at
    C:/Documents and Settings/me called "stave.ico" with the contents
    spawned within ImageIcon icon.

    That's it.

    Phil

    Daniel Pitts wrote:
    > wrote:
    > > I am about to throw in the towel on Java at this point! Too many
    > > questions!
    > >
    > > How in the world do you do this one (again, in my native PHP it's
    > > extremely easy:
    > >
    > > $fileID = @fopen('/path/to/your/new/file', 'wb');
    > > if ($fileID) {
    > > @fputs($fileID,
    > > file_get_contents('http://www.domain.com/someimage.jpg'));
    > > @fclose($fileID);
    > > }
    > >

    > [snip]
    > > public static void download(URL url, File file) throws IOException
    > > {
    > > InputStream in = url.openStream();
    > > FileOutputStream out = new FileOutputStream(file);
    > > byte[] b = new byte[1024];
    > > int len;
    > > while((len = in.read(b)) != -1) {
    > > out.write(b, 0, len);
    > > }
    > > out.close();
    > > }
    > >
    > > public static void toFile(File file, Object contents) throws
    > > IOException {
    > > FileOutputStream out = new FileOutputStream(file);
    > > out.write(contents.getBytes());
    > > }
    > > }
    > >
    > > This of course fails, but I have absolutely no idea how to get the
    > > ImageIcon object "contents" to write into the file "/path/to/my/file".
    > >
    > > Phil

    > Hmm, are you trying to save an image (such as a jpeg or gif), or
    > actually trying to serialize the ImageIcon object to a file? It looks
    > like you are simply trying do download data from a URL (thats what your
    > PHP code does). your toFile method can not do that if you have just any
    > Object. Only certain ones allow you to get at the byte[] contents. If
    >
    > What doesn't work about your download method?
    > Other than the fact that you forget call "in.close()" in a failsafe
    > manor (see below)
    > If that is all you really want to do, then it is the best way.
    > However, if you have an Image object, look into ImageIO:
    > <http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageIO.html> It
    > might do what you want.
    >
    >
    > -- This is a safer way to save a binary file from a URL:
    >
    > public static void download(URL url, File file) throws IOException {
    > final InputStream in = url.openStream();
    > try {
    > final OutputStream out = new FileOutputStream(file);
    > try {
    > final byte[] b = new byte[1024];
    > int len;
    > while((len = in.read(b)) != -1) {
    > out.write(b, 0, len);
    > }
    > } finally {
    > out.close();
    > }
    > } finally {
    > in.close();
    > }
    > }
    , Jan 16, 2007
    #3
  4. wrote:
    > I hope this makes it more clear:
    >
    > *update* still fails, can't fix it this time..
    >
    > Here are my methods
    >
    > public static void toFile(File file, Object contents) throws
    > IOException {
    > FileOutputStream out = new FileOutputStream(file);
    > out.write(contents.toString().getBytes());
    > }
    >
    > public static void toFile(String filePath, Object contents) throws
    > IOException {
    > File file = new File(filePath);
    > toFile(file, contents);
    > }
    > }
    >
    > I get this exception thrown:
    >
    > file:\C:\Documents%20and%20Settings\me\stave.ico (The filename,
    > directory name, or volume label syntax is incorrect)
    >
    > I have no idea what to do. All I want to do is create an icon at
    > C:/Documents and Settings/me called "stave.ico" with the contents
    > spawned within ImageIcon icon.
    >
    > That's it.
    >
    > Phil
    >
    > Daniel Pitts wrote:
    >> wrote:
    >>> I am about to throw in the towel on Java at this point! Too many
    >>> questions!
    >>>
    >>> How in the world do you do this one (again, in my native PHP it's
    >>> extremely easy:
    >>>
    >>> $fileID = @fopen('/path/to/your/new/file', 'wb');
    >>> if ($fileID) {
    >>> @fputs($fileID,
    >>> file_get_contents('http://www.domain.com/someimage.jpg'));
    >>> @fclose($fileID);
    >>> }
    >>>

    >> [snip]
    >>> public static void download(URL url, File file) throws IOException
    >>> {
    >>> InputStream in = url.openStream();
    >>> FileOutputStream out = new FileOutputStream(file);
    >>> byte[] b = new byte[1024];
    >>> int len;
    >>> while((len = in.read(b)) != -1) {
    >>> out.write(b, 0, len);
    >>> }
    >>> out.close();
    >>> }
    >>>
    >>> public static void toFile(File file, Object contents) throws
    >>> IOException {
    >>> FileOutputStream out = new FileOutputStream(file);
    >>> out.write(contents.getBytes());
    >>> }
    >>> }
    >>>
    >>> This of course fails, but I have absolutely no idea how to get the
    >>> ImageIcon object "contents" to write into the file "/path/to/my/file".
    >>>
    >>> Phil

    >> Hmm, are you trying to save an image (such as a jpeg or gif), or
    >> actually trying to serialize the ImageIcon object to a file? It looks
    >> like you are simply trying do download data from a URL (thats what your
    >> PHP code does). your toFile method can not do that if you have just any
    >> Object. Only certain ones allow you to get at the byte[] contents. If
    >>
    >> What doesn't work about your download method?
    >> Other than the fact that you forget call "in.close()" in a failsafe
    >> manor (see below)
    >> If that is all you really want to do, then it is the best way.
    >> However, if you have an Image object, look into ImageIO:
    >> <http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageIO.html> It
    >> might do what you want.
    >>
    >>
    >> -- This is a safer way to save a binary file from a URL:
    >>
    >> public static void download(URL url, File file) throws IOException {
    >> final InputStream in = url.openStream();
    >> try {
    >> final OutputStream out = new FileOutputStream(file);
    >> try {
    >> final byte[] b = new byte[1024];
    >> int len;
    >> while((len = in.read(b)) != -1) {
    >> out.write(b, 0, len);
    >> }
    >> } finally {
    >> out.close();
    >> }
    >> } finally {
    >> in.close();
    >> }
    >> }

    >


    You are not being clear about what you want to do. If you want to read
    an image file from the local disk or a URL, use ImageIO.read(). If you
    want to display that image use your ImageIcon component to do that.
    There is no more to it that that.

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Jan 16, 2007
    #4
  5. Guest

    I want to create a file. Any kind of file, a .ico, .xls, .pdf,
    ..whatever, whose contents will be determined by my inserting them into
    the file I create.

    Just like in PHP @fread($fileID, $contents);

    Phil

    Knute Johnson wrote:
    > wrote:
    > > I hope this makes it more clear:
    > >
    > > *update* still fails, can't fix it this time..
    > >
    > > Here are my methods
    > >
    > > public static void toFile(File file, Object contents) throws
    > > IOException {
    > > FileOutputStream out = new FileOutputStream(file);
    > > out.write(contents.toString().getBytes());
    > > }
    > >
    > > public static void toFile(String filePath, Object contents) throws
    > > IOException {
    > > File file = new File(filePath);
    > > toFile(file, contents);
    > > }
    > > }
    > >
    > > I get this exception thrown:
    > >
    > > file:\C:\Documents%20and%20Settings\me\stave.ico (The filename,
    > > directory name, or volume label syntax is incorrect)
    > >
    > > I have no idea what to do. All I want to do is create an icon at
    > > C:/Documents and Settings/me called "stave.ico" with the contents
    > > spawned within ImageIcon icon.
    > >
    > > That's it.
    > >
    > > Phil
    > >
    > > Daniel Pitts wrote:
    > >> wrote:
    > >>> I am about to throw in the towel on Java at this point! Too many
    > >>> questions!
    > >>>
    > >>> How in the world do you do this one (again, in my native PHP it's
    > >>> extremely easy:
    > >>>
    > >>> $fileID = @fopen('/path/to/your/new/file', 'wb');
    > >>> if ($fileID) {
    > >>> @fputs($fileID,
    > >>> file_get_contents('http://www.domain.com/someimage.jpg'));
    > >>> @fclose($fileID);
    > >>> }
    > >>>
    > >> [snip]
    > >>> public static void download(URL url, File file) throws IOException
    > >>> {
    > >>> InputStream in = url.openStream();
    > >>> FileOutputStream out = new FileOutputStream(file);
    > >>> byte[] b = new byte[1024];
    > >>> int len;
    > >>> while((len = in.read(b)) != -1) {
    > >>> out.write(b, 0, len);
    > >>> }
    > >>> out.close();
    > >>> }
    > >>>
    > >>> public static void toFile(File file, Object contents) throws
    > >>> IOException {
    > >>> FileOutputStream out = new FileOutputStream(file);
    > >>> out.write(contents.getBytes());
    > >>> }
    > >>> }
    > >>>
    > >>> This of course fails, but I have absolutely no idea how to get the
    > >>> ImageIcon object "contents" to write into the file "/path/to/my/file".
    > >>>
    > >>> Phil
    > >> Hmm, are you trying to save an image (such as a jpeg or gif), or
    > >> actually trying to serialize the ImageIcon object to a file? It looks
    > >> like you are simply trying do download data from a URL (thats what your
    > >> PHP code does). your toFile method can not do that if you have just any
    > >> Object. Only certain ones allow you to get at the byte[] contents. If
    > >>
    > >> What doesn't work about your download method?
    > >> Other than the fact that you forget call "in.close()" in a failsafe
    > >> manor (see below)
    > >> If that is all you really want to do, then it is the best way.
    > >> However, if you have an Image object, look into ImageIO:
    > >> <http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageIO.html> It
    > >> might do what you want.
    > >>
    > >>
    > >> -- This is a safer way to save a binary file from a URL:
    > >>
    > >> public static void download(URL url, File file) throws IOException {
    > >> final InputStream in = url.openStream();
    > >> try {
    > >> final OutputStream out = new FileOutputStream(file);
    > >> try {
    > >> final byte[] b = new byte[1024];
    > >> int len;
    > >> while((len = in.read(b)) != -1) {
    > >> out.write(b, 0, len);
    > >> }
    > >> } finally {
    > >> out.close();
    > >> }
    > >> } finally {
    > >> in.close();
    > >> }
    > >> }

    > >

    >
    > You are not being clear about what you want to do. If you want to read
    > an image file from the local disk or a URL, use ImageIO.read(). If you
    > want to display that image use your ImageIcon component to do that.
    > There is no more to it that that.
    >
    > --
    >
    > Knute Johnson
    > email s/nospam/knute/
    , Jan 16, 2007
    #5
  6. Guest

    Perhaps this will suit my needs (untested):

    /*
    * FileDownloader.java
    *
    * Created on January 10, 2007, 3:47 PM
    *
    * To change this template, choose Tools | Template Manager
    * and open the template in the editor.
    */

    package FileTools;

    import java.io.*;
    import java.net.*;


    /**
    *
    * @author ppowell-c
    */
    public class FileDownloader implements Serializable {

    public static void download(URL url, File file) throws IOException
    {
    InputStream in = url.openStream();
    FileOutputStream out = new FileOutputStream(file);
    byte[] b = new byte[1024];
    int len;
    while((len = in.read(b)) != -1) {
    out.write(b, 0, len);
    }
    out.close();
    }

    public static void download(String path, File file) throws
    IOException {
    download(new URL(path), file);
    }

    public static void download(String path, Object contents) throws
    IOException {
    ObjectOutputStream out = new ObjectOutputStream(new
    FileOutputStream(path));
    out.writeObject(contents);
    out.close();
    }


    }

    wrote:
    > I want to create a file. Any kind of file, a .ico, .xls, .pdf,
    > .whatever, whose contents will be determined by my inserting them into
    > the file I create.
    >
    > Just like in PHP @fread($fileID, $contents);
    >
    > Phil
    >
    > Knute Johnson wrote:
    > > wrote:
    > > > I hope this makes it more clear:
    > > >
    > > > *update* still fails, can't fix it this time..
    > > >
    > > > Here are my methods
    > > >
    > > > public static void toFile(File file, Object contents) throws
    > > > IOException {
    > > > FileOutputStream out = new FileOutputStream(file);
    > > > out.write(contents.toString().getBytes());
    > > > }
    > > >
    > > > public static void toFile(String filePath, Object contents) throws
    > > > IOException {
    > > > File file = new File(filePath);
    > > > toFile(file, contents);
    > > > }
    > > > }
    > > >
    > > > I get this exception thrown:
    > > >
    > > > file:\C:\Documents%20and%20Settings\me\stave.ico (The filename,
    > > > directory name, or volume label syntax is incorrect)
    > > >
    > > > I have no idea what to do. All I want to do is create an icon at
    > > > C:/Documents and Settings/me called "stave.ico" with the contents
    > > > spawned within ImageIcon icon.
    > > >
    > > > That's it.
    > > >
    > > > Phil
    > > >
    > > > Daniel Pitts wrote:
    > > >> wrote:
    > > >>> I am about to throw in the towel on Java at this point! Too many
    > > >>> questions!
    > > >>>
    > > >>> How in the world do you do this one (again, in my native PHP it's
    > > >>> extremely easy:
    > > >>>
    > > >>> $fileID = @fopen('/path/to/your/new/file', 'wb');
    > > >>> if ($fileID) {
    > > >>> @fputs($fileID,
    > > >>> file_get_contents('http://www.domain.com/someimage.jpg'));
    > > >>> @fclose($fileID);
    > > >>> }
    > > >>>
    > > >> [snip]
    > > >>> public static void download(URL url, File file) throws IOException
    > > >>> {
    > > >>> InputStream in = url.openStream();
    > > >>> FileOutputStream out = new FileOutputStream(file);
    > > >>> byte[] b = new byte[1024];
    > > >>> int len;
    > > >>> while((len = in.read(b)) != -1) {
    > > >>> out.write(b, 0, len);
    > > >>> }
    > > >>> out.close();
    > > >>> }
    > > >>>
    > > >>> public static void toFile(File file, Object contents) throws
    > > >>> IOException {
    > > >>> FileOutputStream out = new FileOutputStream(file);
    > > >>> out.write(contents.getBytes());
    > > >>> }
    > > >>> }
    > > >>>
    > > >>> This of course fails, but I have absolutely no idea how to get the
    > > >>> ImageIcon object "contents" to write into the file "/path/to/my/file".
    > > >>>
    > > >>> Phil
    > > >> Hmm, are you trying to save an image (such as a jpeg or gif), or
    > > >> actually trying to serialize the ImageIcon object to a file? It looks
    > > >> like you are simply trying do download data from a URL (thats what your
    > > >> PHP code does). your toFile method can not do that if you have just any
    > > >> Object. Only certain ones allow you to get at the byte[] contents. If
    > > >>
    > > >> What doesn't work about your download method?
    > > >> Other than the fact that you forget call "in.close()" in a failsafe
    > > >> manor (see below)
    > > >> If that is all you really want to do, then it is the best way.
    > > >> However, if you have an Image object, look into ImageIO:
    > > >> <http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageIO.html> It
    > > >> might do what you want.
    > > >>
    > > >>
    > > >> -- This is a safer way to save a binary file from a URL:
    > > >>
    > > >> public static void download(URL url, File file) throws IOException {
    > > >> final InputStream in = url.openStream();
    > > >> try {
    > > >> final OutputStream out = new FileOutputStream(file);
    > > >> try {
    > > >> final byte[] b = new byte[1024];
    > > >> int len;
    > > >> while((len = in.read(b)) != -1) {
    > > >> out.write(b, 0, len);
    > > >> }
    > > >> } finally {
    > > >> out.close();
    > > >> }
    > > >> } finally {
    > > >> in.close();
    > > >> }
    > > >> }
    > > >

    > >
    > > You are not being clear about what you want to do. If you want to read
    > > an image file from the local disk or a URL, use ImageIO.read(). If you
    > > want to display that image use your ImageIcon component to do that.
    > > There is no more to it that that.
    > >
    > > --
    > >
    > > Knute Johnson
    > > email s/nospam/knute/
    , Jan 17, 2007
    #6
  7. <> wrote:
    >I hope this makes it more clear:
    >
    > *update* still fails, can't fix it this time..
    >
    > Here are my methods
    >
    > public static void toFile(File file, Object contents) throws
    > IOException {
    > FileOutputStream out = new FileOutputStream(file);

    I assume you got the Exception in the line above.
    (Unfortunately you missed to post the complete exception stack trace)

    > out.write(contents.toString().getBytes());
    > }
    >
    > public static void toFile(String filePath, Object contents) throws
    > IOException {
    > File file = new File(filePath);
    > toFile(file, contents);
    > }
    > }
    >
    > I get this exception thrown:
    >
    > file:\C:\Documents%20and%20Settings\me\stave.ico (The filename,
    > directory name, or volume label syntax is incorrect)

    The exception says it all: You try to open a file in directory
    file:\C:\Documents%20and%20Settings\me
    which, of course, doesn't exist.
    What you really want to do, is to open a file in directory
    C:\Documents and Settings\me
    I.e. you have to use
    C:\Documents and Settings\me\stave.ico
    as filePath in your code above.
    >
    > I have no idea what to do. All I want to do is create an icon at
    > C:/Documents and Settings/me called "stave.ico" with the contents
    > spawned within ImageIcon icon.


    Don't confuse URL strings like
    file:\C:\Documents%20and%20Settings\me\stave.ico
    with real Windows file paths like
    C:\Documents and Settings\me\stave.ico

    --
    Thomas
    Thomas Fritsch, Jan 17, 2007
    #7
  8. wrote:
    > Perhaps this will suit my needs (untested):
    >
    > /*
    > * FileDownloader.java
    > *
    > * Created on January 10, 2007, 3:47 PM
    > *
    > * To change this template, choose Tools | Template Manager
    > * and open the template in the editor.
    > */
    >
    > package FileTools;
    >
    > import java.io.*;
    > import java.net.*;
    >
    >
    > /**
    > *
    > * @author ppowell-c
    > */
    > public class FileDownloader implements Serializable {
    >
    > public static void download(URL url, File file) throws IOException
    > {
    > InputStream in = url.openStream();
    > FileOutputStream out = new FileOutputStream(file);
    > byte[] b = new byte[1024];
    > int len;
    > while((len = in.read(b)) != -1) {
    > out.write(b, 0, len);
    > }
    > out.close();
    > }
    >
    > public static void download(String path, File file) throws
    > IOException {
    > download(new URL(path), file);
    > }
    >
    > public static void download(String path, Object contents) throws
    > IOException {
    > ObjectOutputStream out = new ObjectOutputStream(new
    > FileOutputStream(path));
    > out.writeObject(contents);
    > out.close();
    > }
    >
    >
    > }
    >
    > wrote:
    >> I want to create a file. Any kind of file, a .ico, .xls, .pdf,
    >> .whatever, whose contents will be determined by my inserting them into
    >> the file I create.
    >>
    >> Just like in PHP @fread($fileID, $contents);
    >>
    >> Phil
    >>
    >> Knute Johnson wrote:
    >>> wrote:
    >>>> I hope this makes it more clear:
    >>>>
    >>>> *update* still fails, can't fix it this time..
    >>>>
    >>>> Here are my methods
    >>>>
    >>>> public static void toFile(File file, Object contents) throws
    >>>> IOException {
    >>>> FileOutputStream out = new FileOutputStream(file);
    >>>> out.write(contents.toString().getBytes());
    >>>> }
    >>>>
    >>>> public static void toFile(String filePath, Object contents) throws
    >>>> IOException {
    >>>> File file = new File(filePath);
    >>>> toFile(file, contents);
    >>>> }
    >>>> }
    >>>>
    >>>> I get this exception thrown:
    >>>>
    >>>> file:\C:\Documents%20and%20Settings\me\stave.ico (The filename,
    >>>> directory name, or volume label syntax is incorrect)
    >>>>
    >>>> I have no idea what to do. All I want to do is create an icon at
    >>>> C:/Documents and Settings/me called "stave.ico" with the contents
    >>>> spawned within ImageIcon icon.
    >>>>
    >>>> That's it.
    >>>>
    >>>> Phil
    >>>>
    >>>> Daniel Pitts wrote:
    >>>>> wrote:
    >>>>>> I am about to throw in the towel on Java at this point! Too many
    >>>>>> questions!
    >>>>>>
    >>>>>> How in the world do you do this one (again, in my native PHP it's
    >>>>>> extremely easy:
    >>>>>>
    >>>>>> $fileID = @fopen('/path/to/your/new/file', 'wb');
    >>>>>> if ($fileID) {
    >>>>>> @fputs($fileID,
    >>>>>> file_get_contents('http://www.domain.com/someimage.jpg'));
    >>>>>> @fclose($fileID);
    >>>>>> }
    >>>>>>
    >>>>> [snip]
    >>>>>> public static void download(URL url, File file) throws IOException
    >>>>>> {
    >>>>>> InputStream in = url.openStream();
    >>>>>> FileOutputStream out = new FileOutputStream(file);
    >>>>>> byte[] b = new byte[1024];
    >>>>>> int len;
    >>>>>> while((len = in.read(b)) != -1) {
    >>>>>> out.write(b, 0, len);
    >>>>>> }
    >>>>>> out.close();
    >>>>>> }
    >>>>>>
    >>>>>> public static void toFile(File file, Object contents) throws
    >>>>>> IOException {
    >>>>>> FileOutputStream out = new FileOutputStream(file);
    >>>>>> out.write(contents.getBytes());
    >>>>>> }
    >>>>>> }
    >>>>>>
    >>>>>> This of course fails, but I have absolutely no idea how to get the
    >>>>>> ImageIcon object "contents" to write into the file "/path/to/my/file".
    >>>>>>
    >>>>>> Phil
    >>>>> Hmm, are you trying to save an image (such as a jpeg or gif), or
    >>>>> actually trying to serialize the ImageIcon object to a file? It looks
    >>>>> like you are simply trying do download data from a URL (thats what your
    >>>>> PHP code does). your toFile method can not do that if you have just any
    >>>>> Object. Only certain ones allow you to get at the byte[] contents. If
    >>>>>
    >>>>> What doesn't work about your download method?
    >>>>> Other than the fact that you forget call "in.close()" in a failsafe
    >>>>> manor (see below)
    >>>>> If that is all you really want to do, then it is the best way.
    >>>>> However, if you have an Image object, look into ImageIO:
    >>>>> <http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageIO.html> It
    >>>>> might do what you want.
    >>>>>
    >>>>>
    >>>>> -- This is a safer way to save a binary file from a URL:
    >>>>>
    >>>>> public static void download(URL url, File file) throws IOException {
    >>>>> final InputStream in = url.openStream();
    >>>>> try {
    >>>>> final OutputStream out = new FileOutputStream(file);
    >>>>> try {
    >>>>> final byte[] b = new byte[1024];
    >>>>> int len;
    >>>>> while((len = in.read(b)) != -1) {
    >>>>> out.write(b, 0, len);
    >>>>> }
    >>>>> } finally {
    >>>>> out.close();
    >>>>> }
    >>>>> } finally {
    >>>>> in.close();
    >>>>> }
    >>>>> }
    >>> You are not being clear about what you want to do. If you want to read
    >>> an image file from the local disk or a URL, use ImageIO.read(). If you
    >>> want to display that image use your ImageIcon component to do that.
    >>> There is no more to it that that.
    >>>
    >>> --
    >>>
    >>> Knute Johnson
    >>> email s/nospam/knute/

    >


    Well I don't have a clue about PHP but if it works so well why don't you
    just use it?

    Java Objects are not all Serializable. For example the Image object is
    not. You can't do ObjectInputStream/OutputStream operations if they are
    not Serializable.

    You can certainly read the data bytes from the InputStream of the URL
    and save those bytes to a file. In fact your code to do that looks fine.

    So to ask the question again, in what format is your data? Is it in
    some file format or is it Java Objects?

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Jan 17, 2007
    #8
  9. Guest

    Thomas Fritsch wrote:
    > <> wrote:
    > >I hope this makes it more clear:
    > >
    > > *update* still fails, can't fix it this time..
    > >
    > > Here are my methods
    > >
    > > public static void toFile(File file, Object contents) throws
    > > IOException {
    > > FileOutputStream out = new FileOutputStream(file);

    > I assume you got the Exception in the line above.
    > (Unfortunately you missed to post the complete exception stack trace)
    >
    > > out.write(contents.toString().getBytes());
    > > }
    > >
    > > public static void toFile(String filePath, Object contents) throws
    > > IOException {
    > > File file = new File(filePath);
    > > toFile(file, contents);
    > > }
    > > }
    > >
    > > I get this exception thrown:
    > >
    > > file:\C:\Documents%20and%20Settings\me\stave.ico (The filename,
    > > directory name, or volume label syntax is incorrect)

    > The exception says it all: You try to open a file in directory
    > file:\C:\Documents%20and%20Settings\me
    > which, of course, doesn't exist.
    > What you really want to do, is to open a file in directory
    > C:\Documents and Settings\me
    > I.e. you have to use
    > C:\Documents and Settings\me\stave.ico
    > as filePath in your code above.


    Then perhaps this might be a better choice:

    /*
    * FileDownloader.java
    *
    * Created on January 10, 2007, 3:47 PM
    *
    * To change this template, choose Tools | Template Manager
    * and open the template in the editor.
    */

    package FileTools;

    import java.io.*;
    import java.net.*;


    /**
    *
    * @author ppowell-c
    */
    public class FileDownloader implements Serializable {

    /** Creates a new instance of FileDownloader */
    public FileDownloader() {}

    public static void download(URL url, File file) throws IOException
    {
    InputStream in = url.openStream();
    FileOutputStream out = new FileOutputStream(file);
    byte[] b = new byte[1024];
    int len;
    while((len = in.read(b)) != -1) {
    out.write(b, 0, len);
    }
    out.close();
    }

    public static void download(String path, File file) throws
    IOException {
    download(new URL(path), file);
    }

    public static void download(String path, Object contents) throws
    IOException {
    ObjectOutputStream out = new ObjectOutputStream(new
    FileOutputStream(path));
    out.writeObject(contents);
    out.close();
    }


    }

    What do you think?

    Phil
    > >
    > > I have no idea what to do. All I want to do is create an icon at
    > > C:/Documents and Settings/me called "stave.ico" with the contents
    > > spawned within ImageIcon icon.

    >
    > Don't confuse URL strings like
    > file:\C:\Documents%20and%20Settings\me\stave.ico
    > with real Windows file paths like
    > C:\Documents and Settings\me\stave.ico
    >
    > --
    > Thomas
    , Jan 17, 2007
    #9
  10. wrote:
    > Thomas Fritsch wrote:
    >> <> wrote:
    >>> I hope this makes it more clear:
    >>>
    >>> *update* still fails, can't fix it this time..
    >>>
    >>> Here are my methods
    >>>
    >>> public static void toFile(File file, Object contents) throws
    >>> IOException {
    >>> FileOutputStream out = new FileOutputStream(file);

    >> I assume you got the Exception in the line above.
    >> (Unfortunately you missed to post the complete exception stack trace)
    >>
    >>> out.write(contents.toString().getBytes());
    >>> }
    >>>
    >>> public static void toFile(String filePath, Object contents) throws
    >>> IOException {
    >>> File file = new File(filePath);
    >>> toFile(file, contents);
    >>> }
    >>> }
    >>>
    >>> I get this exception thrown:
    >>>
    >>> file:\C:\Documents%20and%20Settings\me\stave.ico (The filename,
    >>> directory name, or volume label syntax is incorrect)

    >> The exception says it all: You try to open a file in directory
    >> file:\C:\Documents%20and%20Settings\me
    >> which, of course, doesn't exist.
    >> What you really want to do, is to open a file in directory
    >> C:\Documents and Settings\me
    >> I.e. you have to use
    >> C:\Documents and Settings\me\stave.ico
    >> as filePath in your code above.

    >
    > Then perhaps this might be a better choice:
    >
    > /*
    > * FileDownloader.java
    > *
    > * Created on January 10, 2007, 3:47 PM
    > *
    > * To change this template, choose Tools | Template Manager
    > * and open the template in the editor.
    > */
    >
    > package FileTools;
    >
    > import java.io.*;
    > import java.net.*;
    >
    >
    > /**
    > *
    > * @author ppowell-c
    > */
    > public class FileDownloader implements Serializable {

    Why is it Serializable? There is no data.
    >
    > /** Creates a new instance of FileDownloader */
    > public FileDownloader() {}
    >

    This method is fine.
    > public static void download(URL url, File file) throws IOException
    > {
    > InputStream in = url.openStream();
    > FileOutputStream out = new FileOutputStream(file);
    > byte[] b = new byte[1024];
    > int len;
    > while((len = in.read(b)) != -1) {
    > out.write(b, 0, len);
    > }
    > out.close();
    > }
    >
    > public static void download(String path, File file) throws
    > IOException {
    > download(new URL(path), file);
    > }
    >

    This will work only if the Object is Serializable
    > public static void download(String path, Object contents) throws
    > IOException {
    > ObjectOutputStream out = new ObjectOutputStream(new
    > FileOutputStream(path));
    > out.writeObject(contents);
    > out.close();
    > }
    >
    >
    > }
    >
    > What do you think?
    >


    I think you aren't listening.

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Jan 17, 2007
    #10
  11. Guest

    Knute Johnson wrote:
    > wrote:
    > > Perhaps this will suit my needs (untested):
    > >
    > > /*
    > > * FileDownloader.java
    > > *
    > > * Created on January 10, 2007, 3:47 PM
    > > *
    > > * To change this template, choose Tools | Template Manager
    > > * and open the template in the editor.
    > > */
    > >
    > > package FileTools;
    > >
    > > import java.io.*;
    > > import java.net.*;
    > >
    > >
    > > /**
    > > *
    > > * @author ppowell-c
    > > */
    > > public class FileDownloader implements Serializable {
    > >
    > > public static void download(URL url, File file) throws IOException
    > > {
    > > InputStream in = url.openStream();
    > > FileOutputStream out = new FileOutputStream(file);
    > > byte[] b = new byte[1024];
    > > int len;
    > > while((len = in.read(b)) != -1) {
    > > out.write(b, 0, len);
    > > }
    > > out.close();
    > > }
    > >
    > > public static void download(String path, File file) throws
    > > IOException {
    > > download(new URL(path), file);
    > > }
    > >
    > > public static void download(String path, Object contents) throws
    > > IOException {
    > > ObjectOutputStream out = new ObjectOutputStream(new
    > > FileOutputStream(path));
    > > out.writeObject(contents);
    > > out.close();
    > > }
    > >
    > >
    > > }
    > >
    > > wrote:
    > >> I want to create a file. Any kind of file, a .ico, .xls, .pdf,
    > >> .whatever, whose contents will be determined by my inserting them into
    > >> the file I create.
    > >>
    > >> Just like in PHP @fread($fileID, $contents);
    > >>
    > >> Phil
    > >>
    > >> Knute Johnson wrote:
    > >>> wrote:
    > >>>> I hope this makes it more clear:
    > >>>>
    > >>>> *update* still fails, can't fix it this time..
    > >>>>
    > >>>> Here are my methods
    > >>>>
    > >>>> public static void toFile(File file, Object contents) throws
    > >>>> IOException {
    > >>>> FileOutputStream out = new FileOutputStream(file);
    > >>>> out.write(contents.toString().getBytes());
    > >>>> }
    > >>>>
    > >>>> public static void toFile(String filePath, Object contents) throws
    > >>>> IOException {
    > >>>> File file = new File(filePath);
    > >>>> toFile(file, contents);
    > >>>> }
    > >>>> }
    > >>>>
    > >>>> I get this exception thrown:
    > >>>>
    > >>>> file:\C:\Documents%20and%20Settings\me\stave.ico (The filename,
    > >>>> directory name, or volume label syntax is incorrect)
    > >>>>
    > >>>> I have no idea what to do. All I want to do is create an icon at
    > >>>> C:/Documents and Settings/me called "stave.ico" with the contents
    > >>>> spawned within ImageIcon icon.
    > >>>>
    > >>>> That's it.
    > >>>>
    > >>>> Phil
    > >>>>
    > >>>> Daniel Pitts wrote:
    > >>>>> wrote:
    > >>>>>> I am about to throw in the towel on Java at this point! Too many
    > >>>>>> questions!
    > >>>>>>
    > >>>>>> How in the world do you do this one (again, in my native PHP it's
    > >>>>>> extremely easy:
    > >>>>>>
    > >>>>>> $fileID = @fopen('/path/to/your/new/file', 'wb');
    > >>>>>> if ($fileID) {
    > >>>>>> @fputs($fileID,
    > >>>>>> file_get_contents('http://www.domain.com/someimage.jpg'));
    > >>>>>> @fclose($fileID);
    > >>>>>> }
    > >>>>>>
    > >>>>> [snip]
    > >>>>>> public static void download(URL url, File file) throws IOException
    > >>>>>> {
    > >>>>>> InputStream in = url.openStream();
    > >>>>>> FileOutputStream out = new FileOutputStream(file);
    > >>>>>> byte[] b = new byte[1024];
    > >>>>>> int len;
    > >>>>>> while((len = in.read(b)) != -1) {
    > >>>>>> out.write(b, 0, len);
    > >>>>>> }
    > >>>>>> out.close();
    > >>>>>> }
    > >>>>>>
    > >>>>>> public static void toFile(File file, Object contents) throws
    > >>>>>> IOException {
    > >>>>>> FileOutputStream out = new FileOutputStream(file);
    > >>>>>> out.write(contents.getBytes());
    > >>>>>> }
    > >>>>>> }
    > >>>>>>
    > >>>>>> This of course fails, but I have absolutely no idea how to get the
    > >>>>>> ImageIcon object "contents" to write into the file "/path/to/my/file".
    > >>>>>>
    > >>>>>> Phil
    > >>>>> Hmm, are you trying to save an image (such as a jpeg or gif), or
    > >>>>> actually trying to serialize the ImageIcon object to a file? It looks
    > >>>>> like you are simply trying do download data from a URL (thats what your
    > >>>>> PHP code does). your toFile method can not do that if you have just any
    > >>>>> Object. Only certain ones allow you to get at the byte[] contents. If
    > >>>>>
    > >>>>> What doesn't work about your download method?
    > >>>>> Other than the fact that you forget call "in.close()" in a failsafe
    > >>>>> manor (see below)
    > >>>>> If that is all you really want to do, then it is the best way.
    > >>>>> However, if you have an Image object, look into ImageIO:
    > >>>>> <http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageIO.html> It
    > >>>>> might do what you want.
    > >>>>>
    > >>>>>
    > >>>>> -- This is a safer way to save a binary file from a URL:
    > >>>>>
    > >>>>> public static void download(URL url, File file) throws IOException {
    > >>>>> final InputStream in = url.openStream();
    > >>>>> try {
    > >>>>> final OutputStream out = new FileOutputStream(file);
    > >>>>> try {
    > >>>>> final byte[] b = new byte[1024];
    > >>>>> int len;
    > >>>>> while((len = in.read(b)) != -1) {
    > >>>>> out.write(b, 0, len);
    > >>>>> }
    > >>>>> } finally {
    > >>>>> out.close();
    > >>>>> }
    > >>>>> } finally {
    > >>>>> in.close();
    > >>>>> }
    > >>>>> }
    > >>> You are not being clear about what you want to do. If you want to read
    > >>> an image file from the local disk or a URL, use ImageIO.read(). If you
    > >>> want to display that image use your ImageIcon component to do that.
    > >>> There is no more to it that that.
    > >>>
    > >>> --
    > >>>
    > >>> Knute Johnson
    > >>> email s/nospam/knute/

    > >

    >
    > Well I don't have a clue about PHP but if it works so well why don't you
    > just use it?
    >
    > Java Objects are not all Serializable. For example the Image object is
    > not. You can't do ObjectInputStream/OutputStream operations if they are
    > not Serializable.
    >
    > You can certainly read the data bytes from the InputStream of the URL
    > and save those bytes to a file. In fact your code to do that looks fine.
    >
    > So to ask the question again, in what format is your data? Is it in
    > some file format or is it Java Objects?
    >
    > --



    Any format whatsoever. Can be .txt, can be .xls, can be .whatever. It
    can be anything on the planet. If it could be a solid actual object it
    would be that too.

    >
    > Knute Johnson
    > email s/nospam/knute/
    , Jan 17, 2007
    #11
  12. Guest

    Knute Johnson wrote:
    > wrote:
    > > Thomas Fritsch wrote:
    > >> <> wrote:
    > >>> I hope this makes it more clear:
    > >>>
    > >>> *update* still fails, can't fix it this time..
    > >>>
    > >>> Here are my methods
    > >>>
    > >>> public static void toFile(File file, Object contents) throws
    > >>> IOException {
    > >>> FileOutputStream out = new FileOutputStream(file);
    > >> I assume you got the Exception in the line above.
    > >> (Unfortunately you missed to post the complete exception stack trace)
    > >>
    > >>> out.write(contents.toString().getBytes());
    > >>> }
    > >>>
    > >>> public static void toFile(String filePath, Object contents) throws
    > >>> IOException {
    > >>> File file = new File(filePath);
    > >>> toFile(file, contents);
    > >>> }
    > >>> }
    > >>>
    > >>> I get this exception thrown:
    > >>>
    > >>> file:\C:\Documents%20and%20Settings\me\stave.ico (The filename,
    > >>> directory name, or volume label syntax is incorrect)
    > >> The exception says it all: You try to open a file in directory
    > >> file:\C:\Documents%20and%20Settings\me
    > >> which, of course, doesn't exist.
    > >> What you really want to do, is to open a file in directory
    > >> C:\Documents and Settings\me
    > >> I.e. you have to use
    > >> C:\Documents and Settings\me\stave.ico
    > >> as filePath in your code above.

    > >
    > > Then perhaps this might be a better choice:
    > >
    > > /*
    > > * FileDownloader.java
    > > *
    > > * Created on January 10, 2007, 3:47 PM
    > > *
    > > * To change this template, choose Tools | Template Manager
    > > * and open the template in the editor.
    > > */
    > >
    > > package FileTools;
    > >
    > > import java.io.*;
    > > import java.net.*;
    > >
    > >
    > > /**
    > > *
    > > * @author ppowell-c
    > > */
    > > public class FileDownloader implements Serializable {

    > Why is it Serializable? There is no data.
    > >
    > > /** Creates a new instance of FileDownloader */
    > > public FileDownloader() {}
    > >

    > This method is fine.
    > > public static void download(URL url, File file) throws IOException
    > > {
    > > InputStream in = url.openStream();
    > > FileOutputStream out = new FileOutputStream(file);
    > > byte[] b = new byte[1024];
    > > int len;
    > > while((len = in.read(b)) != -1) {
    > > out.write(b, 0, len);
    > > }
    > > out.close();
    > > }
    > >
    > > public static void download(String path, File file) throws
    > > IOException {
    > > download(new URL(path), file);
    > > }
    > >

    > This will work only if the Object is Serializable
    > > public static void download(String path, Object contents) throws
    > > IOException {
    > > ObjectOutputStream out = new ObjectOutputStream(new
    > > FileOutputStream(path));
    > > out.writeObject(contents);
    > > out.close();
    > > }
    > >
    > >
    > > }
    > >
    > > What do you think?
    > >

    >
    > I think you aren't listening.


    Maybe you need to explain it to someone with ADD as if I were a 10-year
    old. Try it that way.

    How do you write to a new file anything, like String content or Image
    content or Excel spreadsheet content.. anything at all, into a new
    file? How do you do it?

    Phil

    >
    > --
    >
    > Knute Johnson
    > email s/nospam/knute/
    , Jan 17, 2007
    #12
  13. > Maybe you need to explain it to someone with ADD as if I were a 10-year
    > old. Try it that way.
    >
    > How do you write to a new file anything, like String content or Image
    > content or Excel spreadsheet content.. anything at all, into a new
    > file? How do you do it?
    >
    > Phil
    >


    String data is written with a Writer, Object data is written with an
    ObjectOutputStream and byte data is written with an OutputStream.

    Where is your data coming from?

    The web?

    A file on a disk?

    A Java program?

    Is it a byte stream?

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Jan 17, 2007
    #13
  14. Knute Johnson wrote:
    >> Maybe you need to explain it to someone with ADD as if I were a 10-year
    >> old. Try it that way.
    >>
    >> How do you write to a new file anything, like String content or Image
    >> content or Excel spreadsheet content.. anything at all, into a new
    >> file? How do you do it?
    >>
    >> Phil
    >>

    >
    > String data is written with a Writer, Object data is written with an
    > ObjectOutputStream and byte data is written with an OutputStream.
    >
    > Where is your data coming from?
    >
    > The web?
    >
    > A file on a disk?
    >
    > A Java program?
    >
    > Is it a byte stream?
    >


    I shouldn't really jump in and answer the question for the OP but there
    seems to be an impedance mismatch in the communications channel.

    AIUI the OP wants

    a) Use HTTP to obtain an image from a website somewhere.
    e.g. http://foo.example.com/img/frog.jpg

    b) Write the obtained data to a local disk file in such a way that it
    constitutes a valid image file that can be read by common image viewing
    and editing software. e.g. to c:\frog.jpg as a valid JPEG image file.

    I don't think he cares how the data is temporarily represented in Java.

    Followups set to comp.lang.java.help only (dropping .programmer)
    RedGrittyBrick, Jan 17, 2007
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. terry

    JTable ImageIcon

    terry, Aug 5, 2003, in forum: Java
    Replies:
    2
    Views:
    4,059
    Roedy Green
    Aug 6, 2003
  2. Cheok Yan Cheng

    ImageIcon.paintIcon wont work in JApplet

    Cheok Yan Cheng, Aug 13, 2003, in forum: Java
    Replies:
    0
    Views:
    1,749
    Cheok Yan Cheng
    Aug 13, 2003
  3. Phil...

    How to close ImageIcon file

    Phil..., Oct 9, 2003, in forum: Java
    Replies:
    1
    Views:
    494
    William Brogden
    Oct 9, 2003
  4. tiewknvc9

    ImageIcon to Image in a jar file?

    tiewknvc9, Aug 14, 2006, in forum: Java
    Replies:
    3
    Views:
    1,060
  5. carmelo
    Replies:
    0
    Views:
    945
    carmelo
    May 7, 2009
Loading...

Share This Page