// Example using Java NIO for efficiency public static void downloadAndRename(String urlStr, String targetPath) throws IOException { URL url = new URL(urlStr); try (ReadableByteChannel rbc = Channels.newChannel(url.openStream()); FileOutputStream fos = new FileOutputStream(targetPath)) { fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); } } Use code with caution.
It can replace an existing file if you use the REPLACE_EXISTING option. Syntax:
If the file already exists on your system (perhaps downloaded automatically by a browser or a different process), you can use these methods to change its name:
Uses FileUtils.copyURLToFile() to handle the download in a single line of code. 2. How to Download and Save with a New Name
// Example using Java NIO for efficiency public static void downloadAndRename(String urlStr, String targetPath) throws IOException { URL url = new URL(urlStr); try (ReadableByteChannel rbc = Channels.newChannel(url.openStream()); FileOutputStream fos = new FileOutputStream(targetPath)) { fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); } } Use code with caution.
It can replace an existing file if you use the REPLACE_EXISTING option. Syntax:
If the file already exists on your system (perhaps downloaded automatically by a browser or a different process), you can use these methods to change its name:
Uses FileUtils.copyURLToFile() to handle the download in a single line of code. 2. How to Download and Save with a New Name