renameTo won't move file
Hello,
I am trying to move a file. in the root dir it works, but when im working in sub dir it renameTo returns me false. i tried everything including checking the security and it has no problems. here is the code:
File newDir = new File(dir,artist);
File dest = new File(newDir,file.getName());
System.out.println(file.renameTo(dest));
thank you!
[384 byte] By [
renameToa] at [2007-11-26 18:35:52]

RenameTo can fail for a couple of reasons, for example:
1. One or more directories on the target path don't exist. Method renameTo will not create them.
2. The target files already exists. It's platform dependent whether or not the file will be overwritten or the renaming will simply fail.
Here's a little demo that tries to overcome some of the stumbling blocks rename may face:
1. It verifies that the source file exists.
2. It creates directories for the target file, if needed.
3. It deletes the target file if it already exists.
import java.io.*;
public class Test {
public static void main(String[] args) {
String dir1 = "C:\\";
String dir2 = "C:\\temp";
String filename ="foo.txt";
File src = new File(dir1, filename);
File tgt = new File(dir2, src.getName());
renameTo(src, tgt);
}
public static void renameTo(File src, File tgt) {
if (src.exists()) {
File tgtParent = tgt.getParentFile();
if (!tgtParent.exists()) {
boolean mkdirs = tgtParent.mkdirs();
System.out.format("mkdirs = %b%n", mkdirs);
}
if (tgt.exists()) {
System.out.format("%s exists%n", tgt);
boolean delete = tgt.delete();
System.out.format("delete target = %b%n", delete);
}
boolean renameTo = src.renameTo(tgt);
System.out.format("renameTo = %b%n", renameTo);
} else {
System.out.format("%s doesn't exist%n", src);
}
}
}
I think that you may be having the same problem that I am. http://forum.java.sun.com/thread.jspa?threadID=5137243&tstart=0It could be write protected. What web/application server are you using.Mike
hey thanks
the dir is created in the program and i can see it isnt write protected couse i can write there using createNewFile();
it is NTFS FS, but i think i know what causes the prob - tell me if im wrong:
does it matter if the build the File object like this:
String dir1 = "c:/";
String dir2 = "mp3";
String dir3 = "new";
filename="test.mp3";
File file1 = new File (dir1);
File file2 = new File (file1,dir2);
File file3 = new File (file2,dir3);
File file4 = new File (file3,filename);
does it matter?
here is the program
import java.io.File;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Main {
public static void main(String[] args) {
//Define the start dir
File startDir = new File("c:/mp3");
//Creating mp3 extention filter
FileFilter filter = new FileNameExtensionFilter("Mpeg3 layer","mp3");
//The list of the files in the current dir
String[] filesInDir = startDir.list();
//Looping on the files in the current dir
for (int i=0; i<filesInDir.length; i++){
File newFile = new File(startDir,filesInDir[i]);
System.out.println("Reading: " + newFile);
if(filter.accept(newFile) && newFile.isFile()){
readFile(startDir,newFile);
}else {
readDir(newFile, filter);
}
}
}
private static void readFile(File dir, File file){
ID3 mp3File = new ID3(file);
String artist = null;
try{
artist = mp3File.getArtist();
System.out.println("Artist found: " + artist);
}catch (NoID3TagException e){
System.out.println("WARNING: No artist or IO error");
return;
}
//Checking if the current song sitting in the artist folder already
if(dir.getPath().substring(dir.getPath().indexOf("\\")+1).equals(artist.toLowerCase())) return;
File newDir = new File(dir.getPath(),artist);
System.out.println(newDir.mkdir() ? "Creating dir" : "Dir exists" );
try{
System.out.println(file.renameTo(new File(newDir.getPath(),file.getName())));
}catch(SecurityException e){
System.out.println("Unable to write to destination - check attributes");
}
}
>
It may not be the same problem. When you get a file that won't rename with the program, can you rename it with Windows Explorer?
sure I can...its working outside of the java perfectly!