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 Java8FileMover {
private static final String SRC_FILE_LOCATION = "/path/to/source/";
private static final String DEST_FILE_LOCATION = "/path/to/destination/directory/";
private static final String FILE_NAME = "file.txt";
public static void main(String[] args) {
Path sourceFile = Path.of(SRC_FILE_LOCATION + FILE_NAME);
Path destinationDirPath = Path.of(DEST_FILE_LOCATION);
try {
Path targetFile = Files.move(
sourceFile,
destinationDirPath.resolve(sourceFile.getFileName()),
StandardCopyOption.REPLACE_EXISTING
);
System.out.println("File moved to location: " + targetFile);
} catch (IOException ioException) {
System.err.println("Error occurred while moving the file: " + ioException.getMessage());
}
}
}
Output: File moved to location:
-
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:
- Hide Navigation Bar from Android Screen Activity - Android
- How to add Multiple Rulers in Sublime Text - Sublime-Text
- VS Code Remove Unused Imports Keyboard Shortcut - HowTos
- [Fix] MySQL No database selected - ERROR 1046 (3D000) - MySQL
- Implementing Insertion Sort Algorithm in Java Program - Java
- Center Align TextView Android Horizontally or Vertically - Android
- Bash - How to check if a Command Failed? - Bash
- How to generate client id and secret to register SharePoint App with OAuth - SharePoint