-2

I have a File named myfile without extension. I want to add an extension to it so it will be myfile.ext. I don't want to open it and save it with an extension. Just "rename" the File class instance. How can I do it?

P.S. If that matters, I need this because I want to use this:

    File myfiletopeon=myfile;
    if (Desktop.isDesktopSupported()) {
        try {
            Desktop.getDesktop().open(myfiletopeon);
        } catch (IOException ex) {
             JOptionPane.showMessageDialog(null,"Problem","Problem",JOptionPane.ERROR_MESSAGE);
        }
    } 

in order to open the file and I need an extension so that the file can be opened

geo
  • 517
  • 1
  • 9
  • 28
  • In Java 7 you can use this: http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#move%28java.nio.file.Path,%20java.nio.file.Path,%20java.nio.file.CopyOption...%29 – pL4Gu33 Feb 10 '14 at 09:28
  • Just googled "java rename" and got this http://stackoverflow.com/questions/1158777/renaming-a-file-using-java Please search before asking. – aalku Feb 10 '14 at 09:28
  • Just rename it with extension – Indra Yadav Feb 10 '14 at 09:30
  • Have a look at http://docs.oracle.com/javase/7/docs/api/java/io/File.html#renameTo(java.io.File) – Karthik Kalyanasundaram Feb 10 '14 at 09:30
  • You have to rename the actual file (e.g. `File#rename()`) because the program opening the file can't see that name if the name exists just within your program. – zapl Feb 10 '14 at 09:30
  • Please do a little searching in google before posting a question here, It might have already been answered. – Gyan Feb 10 '14 at 09:31
  • ok, ok don't hit :) I had in mind there might be a special method that add extensions (=strings) to File names. The solution is `String newFilePath = myfile.getAbsolutePath().replace(myfile.getName(), myfile.getName()+".ext") ;` and then get the new File `File newFile = new File(newFilePath); ` – geo Feb 10 '14 at 10:47

1 Answers1

1

How about using File.renameTo() method?