how to backup file in java?

13,284

Solution 1

Path object is perfect for copying files,
Try this code to copy a file,

Path source = Paths.get("c:\\blabla.txt");
    Path target = Paths.get("c:\\blabla2.txt");
    try {
        Files.copy(source, target);
    } catch (IOException e1) {
        e1.printStackTrace();
    }

Solution 2

If you have to backup a whole folder, you can use this code

public class BackUpFolder {

    public void copy(File sourceLocation, File targetLocation) throws IOException {
        if (sourceLocation.isDirectory()) {
            copyDirectory(sourceLocation, targetLocation);
        } else {
            copyFile(sourceLocation, targetLocation);
        }
    }

    private void copyDirectory(File source, File target) throws IOException {
        if (!target.exists()) {
            target.mkdir();
        }

        for (String f : source.list()) {
            copy(new File(source, f), new File(target, f));
        }
    }

    private void copyFile(File source, File target) throws IOException {
        try (
                InputStream in = new FileInputStream(source);
                OutputStream out = new FileOutputStream(target)) {
            byte[] buf = new byte[1024];
            int length;
            while ((length = in.read(buf)) > 0) {
                out.write(buf, 0, length);
            }
        }
    }

    public static void main(String[] args) {
        try {
            BackUpFolder backUpFolder = new BackUpFolder();
            String location = "./src/edu/abc/locationFiles/daofile"; //File path you are getting from file chooser
            String target = "./src"; //target place you want to patse
            File locFile = new File(location);
            File tarFile = new File(target);
            backUpFolder.copyDirectory(locFile, tarFile);
        } catch (IOException ex) {
            Logger.getLogger(BackUpFolder.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

Solution 3

Start by taking a look at Basic I/O, which explains the basics of Input/OutputStreams and Readers and Writers, which are used to read/write bytes of data from a source to a destination.

If you're using Java 7 or over, you should also take a look at Copying a File or Directory which is part of newer Files and Paths API, which you can find more information about at File I/O (Featuring NIO.2)

Share:
13,284
user3236502
Author by

user3236502

Updated on June 04, 2022

Comments

  • user3236502
    user3236502 almost 2 years

    In reality its just making a copy of a text.txt file. I know how to use file chooser to choose the file but that is as far as my knowledge really goes.

    I can do this:

    public BasicFile()
    {
       JFileChooser choose = new JFileChooser(".");
       int status = choose.showOpenDialog(null);
    
       try
       {
            if (status != JFileChooser.APPROVE_OPTION) throw new IOException();
    
            f = choose.getSelectedFile();
    
            if (!f.exists()) throw new FileNotFoundException();
       }
       catch(FileNotFoundException e)
       {
            display(1, e.toString(), "File not found ....");
       }
       catch(IOException e)
       {
            display(1, e.toString(),  "Approve option was not selected");
       }
    
    }
    
    • java-love
      java-love about 10 years
      Do you want to know how to copy files?
    • user3236502
      user3236502 about 10 years
      Yes, I would love to.
  • java-love
    java-love about 10 years
    I’m really sorry because my first example didn’t work at all, I just have copy it from a tutorial without trying it, but this code I tested and it works,
  • MadProgrammer
    MadProgrammer almost 4 years
    Oh no, I told some one to read the documentation and explore actual offical tutorials 😱 shame on me for answering a broad and open end question with a broad and open ended answer
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels almost 4 years
    OMG, a MadProgrammer comment that triggered the Queen heat detector. Welcome to an esteemed club, since mine trigger this detector all the time (but I've never seen one of yours do this until now)
  • MadProgrammer
    MadProgrammer almost 4 years
    @HovercraftFullOfEels 😆 I've become sarcastic in my old age
  • MadProgrammer
    MadProgrammer almost 4 years
    @HovercraftFullOfEels I've always been that way, just had to turn the filters off :D