There is an online file (i.e. http://www.website.com/information.asp) I need to grab and save to a directory. I know there are several methods for grabbing and reading online files line-by-line (i.e. URL), but is there a way to just download and save the file using Java?
Grab the file and read it line-by-line, as you mention, and save the lines to a local file.
Downloading a file requires you to read it, either way you will have to go through the file in some way. Instead of line by line, you can just read it by bytes from the stream:
BufferedInputStream in = new BufferedInputStream(new Url("http://www.website.com/information.asp").openStream())
byte data[] = new byte[1024];
while(int count = in.read(data,0,1024) != null)
{
out.write(data, 0, count);
}
I found this example which is probably overkill but you can extract the relavant parts yourself.
give a try to Java NIO:
URL google = new URL("http://www.google.it");
ReadableByteChannel rbc = Channels.newChannel(google.openStream());
FileOutputStream fos = new FileOutputStream("google.html");
fos.getChannel().transferFrom(rbc, 0, 1 << 24);
using transferForm()
is potentially much more efficient than a simple loop that reads from the source channel and writes to this channel. Many operating systems can transfer bytes directly from the source channel into the filesystem cache without actually copying them.
Check more about it here.
public void saveUrl(String filename, String urlString) throws MalformedURLException, IOException
{
BufferedInputStream in = null;
FileOutputStream fout = null;
try
{
in = new BufferedInputStream(new URL(urlString).openStream());
fout = new FileOutputStream(filename);
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();
}
}
You'll need to handle exceptions, probably external to this method.
Personally, I've found Apache's HttpClient to be more than capable of everything I've needed to do with regards to this. Here is a great tutorial on using HttpClient