Java Program to Rename a File
Program
import java.io.File;
import java.util.Scanner;
public class FileRename
{
public static void main(String[] args)
{
File src_file = null, dest_file = null;
String src_file_name = null, dest_file_name = null;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the file name: ");
src_file_name = sc.next();
src_file = new File(src_file_name);
System.out.print("Enter the new name: ");
dest_file_name = sc.next();
dest_file = new File(dest_file_name);
if(src_file.renameTo(dest_file))
{
System.out.println("File renamed successfully from " + src_file + " to " + dest_file);
}
else
{
System.out.println("Rename operation failed");
}
}
}
Purpose
The program:
- Allows a user to rename an existing file.
- Uses the
renameTo
method from theFile
class to perform the operation.
1. Import Statements
import java.io.File;
import java.util.Scanner;
java.io.File
: Provides theFile
class for file operations.java.util.Scanner
: Facilitates user input from the console.
2. Class Definition
public class FileRename
Defines the class FileRename
, containing all the logic for renaming a file.
3. Main Method
The main
method contains:
- User interaction for file name inputs.
- Logic for renaming a file.
4. Define File
Objects
File src_file = null, dest_file = null;
String src_file_name = null, dest_file_name = null;
src_file
: Represents the original file.dest_file
: Represents the new file name.src_file_name
anddest_file_name
: Strings to store user input for file names.
5. Get User Input
The program prompts the user for:
- The name of the file to rename.
- The desired new file name.
System.out.print("Enter the file name: ");
src_file_name = sc.next();
System.out.print("Enter the new name: ");
dest_file_name = sc.next();
- The
Scanner
class is used for console input.
6. Create File
Objects
src_file = new File(src_file_name);
dest_file = new File(dest_file_name);
src_file
: Represents the original file path.dest_file
: Represents the new file path.
7. Rename the File
if (src_file.renameTo(dest_file)) {
System.out.println("File renamed successfully from " + src_file + " to " + dest_file);
} else {
System.out.println("Rename operation failed");
}
renameTo
:- Attempts to rename the file.
- Returns
true
if successful,false
otherwise.
- Success Case: Prints a success message.
- Failure Case: Prints an error message.
Key Concepts
-
File
Class:- The
renameTo
method renames a file or directory. - It may fail if:
- The file does not exist.
- The file is in use.
- Permissions are insufficient.
- The destination file already exists.
- The
-
User Interaction:
- The program dynamically accepts file names from the user.
-
Error Handling:
- No explicit exception handling is used; instead, the
renameTo
method’s boolean output indicates success or failure.
- No explicit exception handling is used; instead, the
Output 1
Enter the file name: source.txt
Enter the new name: dest.txt
File renamed successfully from source.txt to dest.txt
Output 2
Enter the file name: source.txt
Enter the new name: dest.txt
Rename operation failed