How do I download and save files from the Java Internet?

Asked 2 years ago, Updated 2 years ago, 165 views

http://www.example.com/information.aspIf you have a file on a site like this, I'd like to save it in a folder. I know I read online files as line-by-line, but I don't know how to download and save them.

java download

2022-09-22 22:28

1 Answers

Write JAVANIO

URL website = new URL("http://www.website.com/information.asp");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("information.html");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

The transferFrom() method is much more efficient than simply turning around and reading the loop because it can transfer bytes into the file system cache without actually copying it.


2022-09-22 22:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.