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,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- How to install Python 3.9 using brew on Mac - Python
- Fix: Windows 10/11 Update Install Error - 0x80070103 - Windows
- How to create Jira API Token for Atlassian Account to connect Jira Cloud REST API - Jira
- Java SE JDBC Select Statement Example - Java
- Where is .zshrc file located in macOS - MacOS
- Align html element at the center of page vertically and horizontally - Html
- How to detect Browser and Operating System Name and Version using JavaScript - JavaScript
- How to Concatenate Strings in Bash Scripting - Bash