There is an online file (such as http://www.example.com/information.asp
) I need to grab and save to a directory. I know there are several methods for grabbing and reading online files (URLs) line-by-line, but is there a way to just download and save the file using Java?
Java – How to download and save a file from the Internet using Java
downloadjava
Related Topic
- Java – Including all the jars in a directory within the Java classpath
- Java – How to call one constructor from another in Java
- Java – How to create an executable JAR with dependencies using Maven
- Java – Download a file with Android, and showing the progress in a ProgressDialog
- Java – How to avoid Java code in JSP files, using JSP 2
- Java – How to create a memory leak in Java
- Java – Static Classes In Java
- Javascript – How to download a file with Node.js (without using third-party libraries)
Best Answer
Give Java NIO a try:
Using
transferFrom()
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.
Note: The third parameter in transferFrom is the maximum number of bytes to transfer.
Integer.MAX_VALUE
will transfer at most 2^31 bytes,Long.MAX_VALUE
will allow at most 2^63 bytes (larger than any file in existence).