0

I cant understand why my code refuses to change the file extension of my txt file to java.

Here's my code:

public static void main(String[] arg) {

 File file  = new File("file.txt"); //File I want to change to .java
 File file2 = new File("file.java"); 

 boolean success = file.renameTo(file2); //boolean to check if successful


 if (success == true)
 {
     System.out.println("file extension change successful");

 }else
 {
     System.out.println("File extension change failed");
 }

}// main

It always prints "file extension failed" each time and I honestly do not understand why. I'm starting to suspect it might be the permissions on my computer. The compiler I use is Eclipse.

FIXED:

THE CAUSE OF THE PROBLEM: I had placed the file I wanted to change, file.txt, in the package folder inside my project folder. C:\Users\Acer\workspace\MyProjectName\src\MyPackageName. As a consequence the file could not be found by the system.

THE SOLUTION: I simply moved the file, file.txt, into the main project folder; C:\Users\Acer\workspace\MyProjectName and this fixed the problem. When I run my program it returns

file extension change successful

.

Thank you all for your help. I really appreciate it.

  • 1
    Try adding in `System.out.println("Exists: " + file.exists());` before you try renaming it. Also make sure the file is not opened by any other application and you have read/write permissions for the file/folder – MadProgrammer Nov 05 '15 at 03:09
  • http://stackoverflow.com/questions/1158777/rename-a-file-using-java?answertab=oldest#tab-top – T D Nguyen Nov 05 '15 at 03:13

2 Answers2

1

The classic Java File API is pretty limited in a number of respects. In this case, you are getting no feedback as to why the move is failing.

My recommendation, if you are on Java 7, would be to switch to use the nio file functionality that was added in that version. Not only are you then making use of a newer, more robust API, but you should get better messaging as to why your copy is failing. Here is equivalent code to the code you posted.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class MoveFile {
  public static void main(String[] arg) {
    Path file  = Paths.get("file.txt"); //File I want to change to .java
    Path file2 = Paths.get("file.java");

    try {
      Files.move(file, file2);
      System.out.println("file extension change successful");
    } catch (IOException e) {
       System.out.println("File extension change failed: " + e.getMessage());
       e.printStackTrace();
    }
  }
}

For awareness, Oracle provides a good reference page for converting legacy file code to the nio code.

M. Justin
  • 14,487
  • 7
  • 91
  • 130
  • Thank you for your help, I used the code you recommended and found the problem to be the that the file was not found. It turns out that I was mistaken in putting it in the package folder: C:\Users\Acer\workspace\MyProjectName\src\MyPackageName. Where-as I actually had to put in the project folder: C:\Users\Acer\workspace\MyProjectName. – SanListon Madzima Nov 05 '15 at 18:16
1

The Source file i.e file.txt should exist in the mentioned path (in your case its should be inside the eclipse project folder) and no file with the destination file name i.e file.java should exist in the mentioned path. After you execute your code it will give file extension change successful and file.txt will be gone, its content will be transferred to the file.java

  • Thank you, I found the problem: I had placed the desired file in the package folder inside the project folder. **The solution was to simply move the desired file into the main project folder: C:\Users\Acer\workspace\MyProjectName.** – SanListon Madzima Nov 05 '15 at 18:20