-1

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?

2 Answers2

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
  • 345
  • 4
  • 15
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
  • 51
  • 1
  • 4