0

I have a

 File file = new File("/home/aa.db");

then I call file.delete();

but I like to save this file before delete with the other name in the other location. - how it is possible to do in code without any visual tools?

I need a copy - because I need to remove init file. Renaming will not do the thing

curiousity
  • 4,703
  • 8
  • 39
  • 59
  • 2
    http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#copy – AJ. Feb 14 '14 at 11:17
  • Why not just rename it? http://stackoverflow.com/questions/1158777/renaming-a-file-using-java – doctorlove Feb 14 '14 at 11:18
  • I have add import nio.file; than I try file. (but there is no copy) than I wrote java.nio.file. (but there is no copy as static method here too) =( – curiousity Feb 14 '14 at 11:22

2 Answers2

1

Here the sample of moving file to new directory -

File file = new File("/home/aa.db");
File dir = new File("dir");

if (file.renameTo(new File(dir, file.getName()))) {
    // processing here
}

Relevant example:

import java.io.File;

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

        File file = new File("C://aa.db");
        File tmpFile = new File("C://temp", file.getName());

        if(file.renameTo(tmpFile)) {
            if(tmpFile.delete()) {
                System.out.println(tmpFile.getName() + " was deleted!");
            } else {
                System.out.println("Delete operation failed.");
            }
        }
    }
}

Output: aa.db was deleted!

-1

Read the data of file(your need) in InputStream and then use this function of File

public static long copy(InputStream in,Path target,CopyOption... options)
                 throws IOException

Copies all bytes from an input stream to a file. On return, the input stream will be at end of stream.

Before this, first you need to create the target path where you want to copy the data.

AJ.
  • 4,526
  • 5
  • 29
  • 41