Java File Backup saveBackup(File file, int backups, String backupPrefix, String backupSuffix, String backupDirectory)

Here you can find the source of saveBackup(File file, int backups, String backupPrefix, String backupSuffix, String backupDirectory)

Description

Saves a backup (optionally numbered) of a file.

License

Open Source License

Parameter

Parameter Description
file A local file
backups The number of backups. Must be >= 1. If > 1, backup files will be numbered.
backupPrefix The backup file name prefix
backupSuffix The backup file name suffix
backupDirectory The directory where to save backups; if null, they will be saved in the same directory as the file itself.

Declaration

public static void saveBackup(File file, int backups,
        String backupPrefix, String backupSuffix, String backupDirectory) 

Method Source Code

//package com.java2s;
/*//  w w w . ja  v  a  2  s  .com
 * MiscUtilities.java - Various miscallaneous utility functions
 * :tabSize=8:indentSize=8:noTabs=false:
 * :folding=explicit:collapseFolds=1:
 *
 * Copyright (C) 1999, 2000, 2001 Slava Pestov
 * Portions copyright (C) 2000 Richard S. Hall
 * Portions copyright (C) 2001 Dirk Moebius
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.io.*;

public class Main {
    /**
     * Saves a backup (optionally numbered) of a file.
     * @param file A local file
     * @param backups The number of backups. Must be >= 1. If > 1, backup
     * files will be numbered.
     * @param backupPrefix The backup file name prefix
     * @param backupSuffix The backup file name suffix
     * @param backupDirectory The directory where to save backups; if null,
     * they will be saved in the same directory as the file itself.
     * @since jEdit 4.0pre1
     */
    public static void saveBackup(File file, int backups,
            String backupPrefix, String backupSuffix, String backupDirectory) {
        if (backupPrefix == null)
            backupPrefix = "";
        if (backupSuffix == null)
            backupSuffix = "";

        String name = file.getName();

        // If backups is 1, create ~ file
        if (backups == 1) {
            file.renameTo(new File(backupDirectory, backupPrefix + name
                    + backupSuffix));
        }
        // If backups > 1, move old ~n~ files, create ~1~ file
        else {
            new File(backupDirectory, backupPrefix + name + backupSuffix
                    + backups + backupSuffix).delete();

            for (int i = backups - 1; i > 0; i--) {
                File backup = new File(backupDirectory, backupPrefix + name
                        + backupSuffix + i + backupSuffix);

                backup.renameTo(new File(backupDirectory, backupPrefix
                        + name + backupSuffix + (i + 1) + backupSuffix));
            }

            file.renameTo(new File(backupDirectory, backupPrefix + name
                    + backupSuffix + "1" + backupSuffix));
        }
    }
}