RenameTo file not working

I am trying to add the extension to the file name selected by JFileChooser , although I cannot get it to work.

This is the code:

 final JFileChooser fc = new JFileChooser(); fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int returnVal = fc.showSaveDialog(null); if (returnVal == JFileChooser.APPROVE_OPTION) { File f = fc.getSelectedFile(); String name =f.getAbsoluteFile()+".txt"; f.renameTo(new File(name)); FileWriter fstream; try { fstream = new FileWriter(f); BufferedWriter out = new BufferedWriter(fstream); out.write("test one"); out.close(); } catch (IOException ex) { Logger.getLogger(AppCore.class.getName()).log(Level.SEVERE, null, ex); } } 

I canโ€™t understand why this is not working. I also tried using getPath () and getCanonicalPath (), but the result is the same. The file is created in the selected directory, although without the .txt extension.

+6
source share
4 answers

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); // renames test1.txt to test2.txt 

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.

+2
source

You can also use the Files.move utility from the Google Guava libraries to rename the file. This is easier than writing your own method.

From the docs:

Moves a file from one path to another. This method can rename the file or move it to another directory, for example, the unix mv command.

+1
source

If you want to rename a file, you must close the entire object (for example, FileReader, FileWriter, FIS, FOSmeans close the read object, and then rename it

+1
source

You are writing the wrong file. When you call renameTo, the current file does not change its path. Try the following:

 final JFileChooser fc = new JFileChooser(); fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int returnVal = fc.showSaveDialog(null); if (returnVal == JFileChooser.APPROVE_OPTION) { File f = fc.getSelectedFile(); String name =f.getAbsoluteFile()+".txt"; File f2 = new File(name); f.renameTo(f2); FileWriter fstream; try { fstream = new FileWriter(f2); BufferedWriter out = new BufferedWriter(fstream); out.write("test one"); out.close(); } catch (IOException ex) { Logger.getLogger(AppCore.class.getName()).log(Level.SEVERE, null, ex); } } 
0
source

Source: https://habr.com/ru/post/912181/


All Articles