user3808470
user3808470

Reputation:

Move a file to a specific folder

I'm trying to move a file to a specified folder but I can't. This is the code:

  public static void moveToRightDirectory(File song, String album) throws IOException {
    if(album.endsWith(" ")) { 
      album = album.substring(0, album.length() - 1);
    }
    String pathDirectory = selectedDir + "\\" + album;
    File dir = new File(pathDirectory);
    System.out.println("dir.exists(): " + dir.exists());
    if(dir.exists()) { 
      Files.move(song.toPath(), dir.toPath(), StandardCopyOption.REPLACE_EXISTING );
      //System.out.println(song.renameTo(dir));
    }
    else { 
      boolean success = (new File(pathDirectory)).mkdirs();
      if(!success) {
        System.out.println("Error creating directory.");
      }
      else {
        Files.move(song.toPath(), dir.toPath(), StandardCopyOption.REPLACE_EXISTING );
        //System.out.println(song.renameTo(dir));
        //FileUtils.moveFile(song, dir);
      }
    }
  }

I know there are other message about this (from these I was inspired) but I wasn't able to solve so I would ask for your help.

I would like to move the song file in the folder dir. To do this I have tried several methods:

How can I fix? Thanks a lot.

Upvotes: 0

Views: 842

Answers (2)

Busturdust
Busturdust

Reputation: 2495

http://www.mkyong.com/java/how-to-move-file-to-another-directory-in-java/

Maybe this will be of assistance

Step 1 : Rename File

import java.io.File;

public class MoveFileExample 
{
    public static void main(String[] args)
    {   
        try{

           File afile =new File("C:\\folderA\\Afile.txt");

           if(afile.renameTo(new File("C:\\folderB\\" + afile.getName()))){
            System.out.println("File is moved successful!");
           }else{
           System.out.println("File is failed to move!");
           }

         }catch(Exception e){
        e.printStackTrace();
    }
}

}

Step 2: Copy and delete

   import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class MoveFileExample 
{
    public static void main(String[] args)
    {   

        InputStream inStream = null;
    OutputStream outStream = null;

        try{

            File afile =new File("C:\\folderA\\Afile.txt");
            File bfile =new File("C:\\folderB\\Afile.txt");

            inStream = new FileInputStream(afile);
            outStream = new FileOutputStream(bfile);

            byte[] buffer = new byte[1024];

            int length;
            //copy the file content in bytes 
            while ((length = inStream.read(buffer)) > 0){

                outStream.write(buffer, 0, length);

            }

            inStream.close();
            outStream.close();

            //delete the original file
            afile.delete();

            System.out.println("File is copied successful!");

        }catch(IOException e){
            e.printStackTrace();
        }
    }
}

Upvotes: 0

Sekkuar
Sekkuar

Reputation: 386

Not an answer to the actual question, but, based on the error message

java.nio.file.InvalidPathException: Trailing char < > at index 55:

I'm guessing your code isn't getting rid of the trailling whitespaces properly.

Try

album = album.trim();

Instead of

if(album.endsWith(" ")) { 
  album = album.substring(0, album.length() - 1);
}

Upvotes: 3

Related Questions