JFileChoose and FileOutputStream, nothing is being saved
Hey I'm pretty lost, hence my username, but I have this code here:
Code java:
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.
Re: JFileChoose and FileOutputStream, nothing is being saved
Are there any exceptions being thrown? If so, please post the full error message
Re: JFileChoose and FileOutputStream, nothing is being saved
Quote:
Originally Posted by
lost
Hey I'm pretty lost, hence my username, but I have this code here:
Code java:
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.
~X(
How do you write a JPanel to a file?
Re: JFileChoose and FileOutputStream, nothing is being saved
Quote:
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
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..