5

I have three directories A, B & C on windows. I have a file that exists in directory A. I want to do the following tasks

  1. Copy it to directory B
  2. Delete it from directory A (This WORKED since the file was not being held by any process)
  3. Copy it to directory C
  4. Delete it from directory B (NOT Work)

Steps 1,2,3 work fine but it does not work with step 4. The file exists and can write, read, execute. When I open windows explorer and try to delete the file in directory B manually, it said the action can't be completed because it's opened in java platform SE binary. Below is my code for copying the file

        FileInputStream in = new FileInputStream(source);
        FileOutputStream out = new FileOutputStream(dest);

        byte[] buf = new byte[1024];
        int len;

        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }

        in.close();
        out.close();

I'm using Java 6. Do you know how I can accomplish step 4?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Andy
  • 195
  • 2
  • 5
  • 13
  • 2
    What's the point of copying it to directory B if you just delete it in the last step? – Josh M Oct 12 '13 at 00:50
  • Check this out: http://stackoverflow.com/questions/991489/i-cant-delete-a-file-in-java – kol Oct 12 '13 at 00:51
  • If A, B, and C are all in the same file system, why not use a rename instead of one of the copy steps? – user207421 Oct 12 '13 at 01:04
  • I have to do something with the file so I have to copy it to directory B. I tried rename before and it did not work either. – Andy Oct 12 '13 at 17:00

3 Answers3

1

Why not use a library like Apache Commons IO (FileUtils)?

http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html

File a = new File("A/file.txt");
File b = new File("B/file.txt");
File c = new File("C/file.txt");
FileUtils.copyFile(a, b);
a.delete();
FileUtils.copyFile(b, c);
b.delete();
Kong
  • 8,792
  • 15
  • 68
  • 98
0

Try This:

Code

       public void foo(){
        File afile =new File("A\\Afile.txt");
        File bfile =new File("B\\Bfile.txt");
        InputStream  inStream = new FileInputStream(afile);
        OutputStream 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();

        System.out.println("File Copied");
        if(afile.delete()){
            System.out.println(file.getName() + " deleted!");
        }else{
            System.out.println("Delete failed.");
        }
      }

Please make sure you use proper try and catch clauses

Bhanu Kaushik
  • 876
  • 6
  • 25
-1

If you know how to open Windows Task Manager then open that and click Processes then you click the word DESCRIPTION at the top then scroll and look for the word JAVA then you right click it and click End Process Tree once you have gone through that then you close Windows Task Manager and go back to File Explorer then you go back to the Java file and delete it or open it to delete whatever you want to delete in the file i hope this helps with your problem