Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: JFileChoose and FileOutputStream, nothing is being saved

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JFileChoose and FileOutputStream, nothing is being saved

    Hey I'm pretty lost, hence my username, but I have this code here:

    package downloadtesting;
    import java.io.*;
    import java.net.*;
    import javax.swing.JFileChooser;
     
    public class FileDownLoader {
        private JFileChooser jfc;
        public boolean saveUrl(String urlString) throws MalformedURLException, IOException {
            BufferedInputStream in = null;
            FileOutputStream fout = null;
        	jfc = new JFileChooser();
            if(jfc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION){
                try {
                    File selectedFile = jfc.getSelectedFile();
                    in = new BufferedInputStream(new URL(urlString).openStream());
                    fout = new FileOutputStream(selectedFile);
                    byte data[] = new byte[1024];
                    int count;
                    while ((count = in.read(data, 0, 1024)) != -1) {
                        fout.write(data, 0, count);
                    }
                }
                finally {
                    if (in != null)
                            in.close();
                    if (fout != null)
                            fout.close();
                }
                return true;
            } else {
                return false;
            }
        }
    }

    Which I'm trying to use to download a .jar file from my shell account for testing, however nothing is even being saved.

    So what I'm trying to do is have this class/method download a jar file from my webhost and save it to where I specify with JFileChooser.showSaveDilog, however nothings even being saved, stuck and help much appreciated.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JFileChoose and FileOutputStream, nothing is being saved

    Are there any exceptions being thrown? If so, please post the full error message

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: JFileChoose and FileOutputStream, nothing is being saved

    Quote Originally Posted by lost View Post
    Hey I'm pretty lost, hence my username, but I have this code here:

    package downloadtesting;
    import java.io.*;
    import java.net.*;
    import javax.swing.JFileChooser;
     
    public class FileDownLoader {
        private JFileChooser jfc;
        public boolean saveUrl(String urlString) throws MalformedURLException, IOException {
            BufferedInputStream in = null;
            FileOutputStream fout = null;
        	jfc = new JFileChooser();
            if(jfc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION){
                try {
                    File selectedFile = jfc.getSelectedFile();
                    in = new BufferedInputStream(new URL(urlString).openStream());
                    fout = new FileOutputStream(selectedFile);
                    byte data[] = new byte[1024];
                    int count;
                    while ((count = in.read(data, 0, 1024)) != -1) {
                        fout.write(data, 0, count);
                    }
                }
                finally {
                    if (in != null)
                            in.close();
                    if (fout != null)
                            fout.close();
                }
                return true;
            } else {
                return false;
            }
        }
    }

    Which I'm trying to use to download a .jar file from my shell account for testing, however nothing is even being saved.

    So what I'm trying to do is have this class/method download a jar file from my webhost and save it to where I specify with JFileChooser.showSaveDilog, however nothings even being saved, stuck and help much appreciated.
    I'm wishing I knew how to save files too. It must be very advanced Java to save them, because I've tried lots of things.



    How do you write a JPanel to a file?

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: JFileChoose and FileOutputStream, nothing is being saved

    How do you write a JPanel to a file?
    You really should start a separate thread for that question. Suffice to say that JPanel implements Serializable. And take a look at ObjectOutputStream.

    db

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JFileChoose and FileOutputStream, nothing is being saved

    Actually, the code works fine... The file I was trying to grab I forgot I had changed the name, sorry about this..