I have been trying to download a file in a target folder and rename it. This should be automatically done. Is this possible? If yes, how should the code be written in Java?
Asked
Active
Viewed 335 times
-1
-
2What have you tried so far.Post your code – KishanCS Jun 13 '17 at 06:42
-
1First download file as explained here https://stackoverflow.com/a/921400/6743203. After downloading you will know how to rename it – Jay Smith Jun 13 '17 at 06:49
2 Answers
1
Not too sure where you are trying to download from but as mentioned this my help: stackoverflow.com/a/921400/6743203
Alternatively refer to: https://www.mkyong.com/java/java-how-to-download-a-file-from-the-internet/
The renaming should look something like this, without a code example though it's hard to say what exactly you need:
import java.io.File;
public class FileRenameExample
{
public static void main(String[] args)
{
File oldFileName =new File("path/to/old_file_name.txt");
File newFileName =new File("path/to/new_file_name.txt");
if(oldFileName.renameTo(newFileName)){
System.out.println("Rename succesful");
}else{
System.out.println("Rename failed");
}
}
}

Eric Semwenda
- 436
- 6
- 16
0
File f = new File("YOUR-PATH/FileName.EXTENSION");
File fNew = new File("YOUR-PATH/NEW-FileName.EXTENSION");
if(f.renameTo(fNew)){
//do something
}
else{
//handle exception or throw custom exception
}

Lakshay Gupta
- 71
- 1
- 4