Let us take a look at how to rename files in Java with examples of Java 7, 8, 9 and 11, and 17.
Example: Java 7package org.code2care.java.examples;
import java.io.File;
public class FileRenameJava7 {
public static void main(String[] args) {
File oldFileName = new File("old_file.csv");
File newFileName = new File("new_file.csv");
if (oldFileName.renameTo(newFileName)) {
System.out.println("File renamed successfully.");
} else {
System.out.println("Failed to rename the file.");
}
}
}
Example: Java 8, 9 or 11
package org.code2care.java.examples;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
public class FileRenameJava8 {
public static void main(String[] args) throws IOException {
Path oldFilePath = Path.of("old_file.csv");
Path newFilePath = Path.of("new_file.csv");
Files.move(oldFilePath, newFilePath, StandardCopyOption.REPLACE_EXISTING);
System.out.println("File renamed successfully.");
}
}
Example: Java 17
package org.code2care.java.examples;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class FileRenameJava17 {
public static void main(String[] args) throws IOException {
Path oldFilePath = Path.of("old_file.csv");
Path newFilePath = Path.of("new_file.csv");
Path oldPath = Path.of("old.txt");
Path newPath = Path.of("new.txt");
Files.move(oldPath, newPath);
System.out.println("File renamed successfully.");
}
}
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- How to create alias in macOS - MacOS
- Save TextEdit file as a PDF - MacOS
- Fix Error CAA50021 - Number of retry attempts exceeds expectation - Microsoft 365 Apps - Microsoft
- scp: ssh: connect to host xxxx port 22: Connection refused Error - Linux
- How to Run Terminal As Admin on Mac - MacOS
- PowerShell Concatenate String Examples - Powershell
- Command to know the Available Memory on Linux System - Linux
- How to shuffle lines randomly in Sublime Text - Sublime-Text