myFile.renameTo(new File("/the/new/place/newName.file"));

File#renameTo does that (it can not only rename, but also move between directories, at least on the same file system).

Renames the file denoted by this abstract pathname.

Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

If you need a more comprehensive solution (such as wanting to move the file between disks), look at Apache Commons FileUtils#moveFile

Answer from Thilo on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › io › move.html
Moving a File or Directory (The Java™ Tutorials > Essential Java Classes > Basic I/O)
See Java Language Changes for a ... removed or deprecated options for all JDK releases. You can move a file or directory by using the move(Path, Path, CopyOption...) method....
🌐
Medium
medium.com › @AlexanderObregon › javas-files-move-method-explained-7dee1287fa92
Java’s Files.move() Method Explained | Medium
September 14, 2024 - The Files.move() method is a powerful and flexible tool used for moving or renaming files and directories in Java applications.
🌐
Baeldung
baeldung.com › home › java › java io › java – rename or move a file
Rename or Move a File in Java | Baeldung
January 5, 2024 - Examples of how to get the size of a file in Java. ... In the examples, we’ll use the following setup, which consists of 2 constants for the source and destination file name and a clean-up step to be able to run the tests multiple times: private final String FILE_TO_MOVE = "src/test/resources/originalFileToMove.txt"; private final String TARGET_FILE = "src/test/resources/targetFileToMove.txt"; @BeforeEach public void createFileToMove() throws IOException { File fileToMove = new File(FILE_TO_MOVE); fileToMove.createNewFile(); } @AfterEach public void cleanUpFiles() { File targetFile = new File(TARGET_FILE); targetFile.delete(); }
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › nio › file › Files.html
Files (Java Platform SE 8 )
April 21, 2026 - SecurityException - In the case of the default provider, and a security manager is installed, the checkRead method is invoked to check read access to the source file, the checkWrite is invoked to check write access to the target file. If a symbolic link is copied the security manager is invoked to check LinkPermission("symbolic"). public static Path move(Path source, Path target, CopyOption...
🌐
GeeksforGeeks
geeksforgeeks.org › java › moving-file-one-directory-another-using-java
Moving a file from one directory to another using Java - GeeksforGeeks
October 21, 2021 - public static Path move(Path source, Path target, CopyOption..options) throws IOException Parameters: source - the path to the file to move target - the path to the target file (may be associated with a different provider to the source path) options - options specifying how the move should be done Returns: the path to the target file ... // Java program to illustrate renaming and // moving a file permanently to a new location import java.io.*; import java.nio.file.Files; import java.nio.file.*; public class Test { public static void main(String[] args) throws IOException { Path temp = Files.move (Paths.get("C:\\Users\\Mayank\\Desktop\\44.txt"), Paths.get("C:\\Users\\Mayank\\Desktop\\dest\\445.txt")); if(temp != null) { System.out.println("File renamed and moved successfully"); } else { System.out.println("Failed to move the file"); } } }
🌐
Dev.java
dev.java › learn › java-io › file-system › move-copy-delete
Manipulating Files and Directories - Dev.java
January 25, 2023 - The copy(Path, OutputStream) method may be used to copy all bytes from a file to an output stream. You can move a file or directory by using the move(Path, Path, CopyOption...) method.
🌐
Reddit
reddit.com › r/javahelp › when i try to move a file to a folder in eclipse, it causes a bunch of errors?
when i try to move a file to a folder in eclipse, it causes ...
December 11, 2020 -

this even happens when i refactor.

there is a little blue "i" in the bottom of the move box and next to it it says "Java references will not be updated" and i assume that's the problem.

frankly though i don't have any other information so hope one of you can help. (:

Top answer
1 of 2
2
The full name of a Java class is dependent on where the file is located. If you simply move a Java file, the package name will be wrong and the Java file will have a compile error. You have to change the package name to match where you put the file. Because the package name changed, every reference to the Java classes in that file will now be wrong, and you will have more compile errors. You have to change all of the imports to match the new package name. In Eclipse, there is a tab called "Problems". That will help you find all the places that broke, so you can fix them.
2 of 2
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
Find elsewhere
🌐
YouTube
youtube.com › watch
Java - #22 - Moving Files
To learn more, please visit the YouTube Help Center: https://www.youtube.com/help
🌐
YouTube
youtube.com › watch
How to Move File From One Directory to Another Directory Using Files Class in Java | File Handling - YouTube
Hi guys, I Aziz welcome you to CoedMaster.In this video, I talked about how to Move file from one directory to another directory using Files class in Java.Fe...
Published   October 6, 2023
🌐
Coderanch
coderanch.com › t › 376007 › java › File-move-Runtime-java-io
File move using Runtime or java.io (Java in General forum at Coderanch)
February 25, 2005 - I found two ways to do this: 1 Calling dos command move from my program or 2 Using standard java.io classes like BufferedInputStream.....read, write Please tell me which will be more better in terms of performance, error handling etc. I was thinking the dos way is better in performance since you dont have to read the file in the stream and then write it back again.
🌐
Oracle
forums.oracle.com › ords › apexds › post › how-does-java-file-i-o-move-files-3327
How does Java file I/O move files? - Oracle Forums
January 21, 2005 - I'm working support for an integration platform, and there's a problem at a customer that seems to crop up once in a blue moon, yet still all to often. Its a bit difficult to track down, because they ...
🌐
Javabrahman
javabrahman.com › corejava › copy-move-file-java-one-location-another-examples
How to copy or move a file in Java from one location ...
package com.javabrahman.corejava; import java.io.IOException; import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; public class CopyMoveFile { public static void main(String args[]) { FileSystem fileSys = FileSystems.getDefault(); Path srcPath = fileSys.getPath("c:\\src_folder\\file1.txt"); Path destPath = fileSys.getPath("c:\\dest_folder\\file1.txt"); try { //TO COPY file1.txt from source to destination folder Files.copy(srcPath, destPath, StandardCopyOption.REPLACE_EXISTING); //TO MOVE file1.txt from source to destination folder Files.move(srcPath, destPath, StandardCopyOption.REPLACE_EXISTING); } catch (IOException ioe) { ioe.printStackTrace(); } } } Explanation of the code
🌐
TutorialsPoint
tutorialspoint.com › article › moving-a-file-from-one-directory-to-another-using-java
Moving a file from one directory to another using Java
June 25, 2020 - We can use Files.move() API to move file from one directory to another. Following is the syntax of the move method. public static Path move(Path source,Path target,CopyOption... options) throws IOException ... import java.io.IOException; import ...
🌐
Java Guides
javaguides.net › 2018 › 07 › how-to-move-file-in-java.html
How to Move a File in Java
June 21, 2024 - The traditional approach involves ... effective, and the choice depends on your specific needs. The File class provides a simple way to move a file by renaming it....
🌐
Coderanch
coderanch.com › t › 672763 › java › Move-file
Move file (Java in General forum at Coderanch)
If there is any exception we need to move the file at that time itself then only the rest of the process will resume. Next file processing will resume. Please let me know your thoughts. ... Not sure what kind of extra thoughts you're expecting. I just showed above one simple way of doing it and that would do exactly what you described, well, you still need to add code line to copy the file from location x to y. For that you can look for a class Files in Java API and there is possibly useful method for you, which reads as move(Path source, Path target, CopyOption...
🌐
CodeJava
codejava.net › java-se › file-io › how-to-rename-move-file-or-directory-in-java
How to rename/move file or directory in Java
Path source = Paths.get("test"); Path newdir = Paths.get("D:/Photo"); Files.move(source, newdir.resolve(source.getFileName()));NOTE: You can move only empty directory and replace existing directory if it is also empty. In either a directory is not empty, a DirectoryNotEmptyException is thrown. ... Nam Ha Minh is certified Java programmer (SCJP and SCWCD).
🌐
Jenkov
jenkov.com › tutorials › java-nio › files.html
Java NIO Files
April 15, 2015 - First the source path and destination path are created. The source path points to the file to move, and the destination path points to where the file should be moved to. Then the Files.move() method is called.
🌐
JavaBeat
javabeat.net › home › how to move a file using java
How to Move a File Using Java
May 6, 2024 - Let’s learn how the renameTo() method works using the below coding example: import java.io.*; import java.nio.file.*; public class MoveFileJava { public static void main(String[] args) throws FileSystemException { File sourceFile = new ...
🌐
HappyCoders.eu
happycoders.eu › java › how-to-list-directory-contents-move-copy-delete-files
How to List, Move, Copy, and Delete Files (Java Tutorial)
November 29, 2024 - Path currentDir = Path.of(System.getProperty("user.home")); Files.list(currentDir).forEach(System.out::println);Code language: Java (java) Let's move on to a more complex case.