1

I am creating an application in which, I am displaying images from specific folder in custom gridview. I want to move clicked item to another folder. I have absolute path of image from gridview as well absolute path of folder to which i want to move my file. The problem is that I donot know how to move a file from folder in which actual images are stored to another folder. Can someone please tell me code or class with help of which I can move a file from folder to folder. I searched a lot on stackoverflow and found some of codes but that weren't working for my case.

1 Answer 1

6

You have 2 ways:

1:

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();
        }
    }
}

2:

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();
        }
    }
}

Code examples take from here: http://www.mkyong.com/java/how-to-move-file-to-another-directory-in-java/

4
  • Good, if it works for you please mark this as an answer, for other people.
    – TomerZ
    Oct 15, 2014 at 16:10
  • sir i m getting error in FileUtils.copyFile(source, destination); FileUtils is underlined with red line
    – Matiullah
    Oct 15, 2014 at 16:13
  • import static java.nio.file.StandardCopyOption.*; this file is not accepting in import so i cant use Files.move(source, target, REPLACE_EXISTING); import static java.nio.file.StandardCopyOption.*; is in red line
    – Matiullah
    Oct 15, 2014 at 16:24
  • tnk u so much sir. i will try it now
    – Matiullah
    Oct 15, 2014 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.