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
🌐
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 › 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.
🌐
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"); } } }
🌐
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...
🌐
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 ...
Find elsewhere
🌐
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.
🌐
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.
🌐
Spiceworks
community.spiceworks.com › programming & development
How can I move all files from one folder to another in PeopleCode using Java? - Programming & Development - Spiceworks Community
January 7, 2025 - Hi everyone, I have been trying to move multiple files from one folder to another in Peoplecode but I can only do it by specifying a file. Is it possible to use a wildcard? I am using java to move the file Local JavaObject &jSource = CreateJavaObject(“java.io.File”, “\hgosnopdat01\op_data_store\op_stage\Financial\PS Batch and Invoice Files\AR\01211_peoplesoft_control.txt”).toPath(); Local JavaObject &jTarget = CreateJavaObject(“java.io.File”, “\hgosnopdat01\op_data_store\op_stage\Financial\I0...
🌐
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...
🌐
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 - But worse than that, it's non-portable -- the command to remove a file will differ not only between different OSs, but possibly even between different versions of the same OS. ... Hi, This is what i use for the move, One problem here, if the source file is very large and still downloading while the moveFile is executed, it returns a false.
🌐
Coderanch
coderanch.com › t › 403173 › java › Moving-file-directory
Moving a file to another directory (Beginning Java forum at Coderanch)
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums · this forum made possible by our volunteer staff, including ... ... Hi im having problems with moving a file to another directory. Im using a JFileChooser for the user to pick a file, then i use the renameTo() method to move it to another directory.
🌐
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.
🌐
Coderanch
coderanch.com › t › 652489 › java › Move-file-specific-folder
Move a file to a specific folder (I/O and Streams forum at Coderanch)
July 11, 2015 - Tony Docherty wrote:Try running a program that does nothing but moves the file. I did a simple program that moves an .mp3 file from one directory to another and it works. This program has no interface so I think the problem is the GUI...
🌐
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.
🌐
Baeldung
baeldung.com › home › java › java io › how to copy a file with java
How to Copy a File with Java | Baeldung
January 4, 2024 - Another common way to copy a file with Java is by using the commons-io library.
🌐
Processing Forum
forum.processing.org › topic › move-files-from-folder-to-folder
Move files from folder ... to folder ... - Processing Forum
- Counting number of files in a folder - Moving them all at one time. - Or move the oldest picture by date. Can you or someone help me with that. import hypermedia.video.*; import java.awt.Rectangle; OpenCV opencv; boolean pictureJustTaken = false; int startTimer; int interval = 5; // interval ...
🌐
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).