It seems to me that all you want to do is change the name of the selected file, and not rename the file in the file system. In this case, you are not using File.renameTo . You just change the File . Something like the following should work:
File f = fc.getSelectedFile(); String name = f.getAbsoluteFile()+".txt"; f = new File(name);
File.renameTo trying to rename a file in the file system. For instance:
File oldFile = new File("test1.txt"); File newFile = new File("test2.txt"); boolean success = oldFile.renameTo(newFile);
After these three lines, success will be true if the file test1.txt can be renamed to test2.txt and false if the renaming was unsuccessful (for example, test1.txt does not exist, open in another process, permission was denied, etc.)
I will be afraid that the renaming you are trying to fail is because you are trying to rename the directory (you are using JFileChooser with the DIRECTORIES_ONLY option). If you have programs that use files in this directory, or they are open on the command line, they will object to renaming this directory.
source share