1

I am trying to transfer the data from old textfile to new textfile. Although the code below is able to transfer successfully, it does not delete the old textfile. May I know why is this so?

private void dataTransfer(String oldFilePath, String newFilePath) {

        byte[] buffer = new byte[10000];
        try {
            FileInputStream fileInput = new FileInputStream(oldFilePath);
            BufferedInputStream bufferedInput = new BufferedInputStream(fileInput);
            FileOutputStream fileOutput = new FileOutputStream(newFilePath);
            BufferedOutputStream bufferedOutput = new BufferedOutputStream(fileOutput);
            while(true) {
                int length = fileInput.read(buffer);
                if(length == -1) {
                    break;
                } else {
                    bufferedOutput.write(buffer);
                    bufferedOutput.flush();
                }   
            }
            fileInput.close();
            bufferedInput.close();
            fileOutput.close();
            bufferedOutput.close();
            File oldFile = new File(oldFilePath);
            oldFile.delete();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(ERROR_TRANSFER_DATA);
        }

    }
stackyyflow
  • 767
  • 3
  • 11
  • 30
  • put technologies tags on question, debug the code and check if file exists before it is deleted – mikus Nov 06 '15 at 09:55
  • Try to check if you have permission to delete the file or if the file exists or not or you can check if the file is locked by some other process! – Rahul Tripathi Nov 06 '15 at 09:56
  • Why do you even copy the file, if you could just [move it](http://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#move-java.nio.file.Path-java.nio.file.Path-java.nio.file.CopyOption...-)? Also why do you flush the output writer at all? `close` does it once and there shouldn't be the need to do it by hand. – fabian Nov 06 '15 at 09:56
  • 1
    Is it an XY-Problem, are your trying to move a file? Then check here: [Move File](http://stackoverflow.com/questions/4645242/how-to-move-file-from-one-location-to-another-location-in-java). Otherwise as mentione before, check if you have the permission to delete this file. Windows is pretty restrictive on many folders. Maybe you can try to delete a file located on your Desktop. – Denis Lukenich Nov 06 '15 at 10:25
  • Do you get any exceptions ? What is the stack trace ? – panagdu Nov 06 '15 at 10:37
  • File.delete returns a boolean. Is that boolean false? – Michael Lloyd Lee mlk Nov 06 '15 at 10:46

5 Answers5

0

For deleting a file it should work fine but for deleting a directory you have to make sure that Directory is Empty.

amrendra
  • 19
  • 6
0

Update the JRE and JDK, make sure you have the rights on the file. Try with a file created by you.

Also, add a catch block for SecurityException

  • JRE and JDK versions don't have much to do here (at least it's very unlikely). `SecurityException` is `RuntimeException`, you will see it if it's thrown. – Jaroslaw Pawlak Nov 06 '15 at 11:38
0

According to Oracle's docs, the delete method does not guarantee that it will delete the file. http://docs.oracle.com/javase/7/docs/api/java/io/File.html#delete()

Deleting a file will fail if:

  • file does not exist
  • there is a lock on that file it might be opened by another process
  • file does not exist on the disk
  • you don't have enough permissions to delete that file (in this case a SecurityException is thrown)
panagdu
  • 2,133
  • 1
  • 21
  • 36
0

You can use the following code block. It works, though don't know. Even without setWritable, it works,

oldFile.setWritable(true);
if(!oldFile.delete()){
     System.out.println("de;eted");
 }
Saikat Biswas
  • 114
  • 1
  • 9
0

I agree with @panagdu that you might not have sufficient rights to delete the file.

Just as a fluke try closing bufferedStream before fileInputStream

like

bufferedInput.close();
fileInput.close();
bufferedOutput.close();
fileOutput.close();

But I don't think this will help.

Test your code for files with sufficient permission. For example Java does not allow the delete() for system files.

Doc
  • 10,831
  • 3
  • 39
  • 63