Android File Backup backup(String strFileName, int iMaxSize, int iRemainSize)

Here you can find the source of backup(String strFileName, int iMaxSize, int iRemainSize)

Description

backup

Parameter

Parameter Description
strFileName String File to check
iMaxSize max size
iRemainSize remain size

Exception

Parameter Description
Exception an exception

Declaration

// //////////////////////////////////////////////////////
public static void backup(String strFileName, int iMaxSize,
        int iRemainSize) throws Exception 

Method Source Code

//package com.java2s;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    /**/*from w w w.  j a  v  a 2s .  com*/
     * @param strFileName
     *            String File to check
     * @param iMaxSize
     *            max size
     * @param iRemainSize
     *            remain size
     * @throws Exception
     */
    // //////////////////////////////////////////////////////
    public static void backup(String strFileName, int iMaxSize,
            int iRemainSize) throws Exception {
        if (iMaxSize <= iRemainSize)
            throw new IllegalArgumentException();
        final java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat(
                "yyyyMMddHHmmss");
        File flSource = new File(strFileName);
        if (flSource.length() > iMaxSize) {
            String strNewName = strFileName + "."
                    + fmt.format(new java.util.Date());
            renameFile(strFileName, strNewName);
            RandomAccessFile fl = null;
            FileOutputStream os = null;
            try {
                os = new FileOutputStream(strFileName);
                fl = new RandomAccessFile(strNewName, "rw");
                fl.seek(fl.length() - iRemainSize);
                byte bt[] = new byte[iRemainSize];
                int iByteRead = fl.read(bt);
                if (iByteRead != iRemainSize)
                    throw new IOException();
                os.write(bt, 0, iByteRead);
                fl.setLength(fl.length() - iRemainSize);
            } finally {
                safeClose(fl);
                safeClose(os);
            }
        }
    }

    public static void backup(String strFileName, int iMaxSize) {
        final java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat(
                "yyyyMMddHHmmss");
        File flSource = new File(strFileName);
        if (flSource.length() > iMaxSize) {
            String strNewName = "";
            if (strFileName.indexOf(".") >= 0)
                strNewName = strFileName.substring(0,
                        strFileName.lastIndexOf("."))
                        + fmt.format(new java.util.Date())
                        + strFileName.substring(strFileName
                                .lastIndexOf("."));
            else
                strNewName = strFileName + fmt.format(new java.util.Date());
            renameFile(strFileName, strNewName);
        }
    }

    /**
     * Backup used for file relation thread
     * 
     * @param strSourcePath
     *            String (Must have '/' at last string)
     * @param strBackupPath
     *            String (Must have '/' at last string)
     * @param strSourceFile
     *            String
     * @param strBackupFile
     *            String
     * @param strBackupStyle
     *            String
     * @throws Exception
     * @return String
     */
    // //////////////////////////////////////////////////////
    public static String backup(String strSourcePath, String strBackupPath,
            String strSourceFile, String strBackupFile,
            String strBackupStyle) throws Exception {
        return backup(strSourcePath, strBackupPath, strSourceFile,
                strBackupFile, strBackupStyle, true);
    }

    public static String backup(String strSourcePath, String strBackupPath,
            String strSourceFile, String strBackupFile,
            String strBackupStyle, boolean bReplace) throws Exception {
        return backup(strSourcePath, strBackupPath, strSourceFile,
                strBackupFile, strBackupStyle, "", bReplace);
    }

    public static String backup(String strSourcePath, String strBackupPath,
            String strSourceFile, String strBackupFile,
            String strBackupStyle, String strAdditionPath) throws Exception {
        return backup(strSourcePath, strBackupPath, strSourceFile,
                strBackupFile, strBackupStyle, strAdditionPath, true);
    }

    public static String backup(String strSourcePath, String strBackupPath,
            String strSourceFile, String strBackupFile,
            String strBackupStyle, String strAdditionPath, boolean bReplace)
            throws Exception {
        // Backup file
        if (strBackupStyle.equals("Delete file")) {
            if (!deleteFile(strSourcePath + strSourceFile))
                throw new Exception("Cannot delete file " + strSourcePath
                        + strSourceFile);
        } else if (strBackupPath.length() > 0) {
            String format = "yyyyMMdd";

            // Backup source file
            String strCurrentDate = "";

            if (strBackupStyle.equals("Monthly")) {
                format = "yyyyMM";
            } else if (strBackupStyle.equals("Yearly")) {
                format = "yyyy";
            }

            SimpleDateFormat dateFormat = new SimpleDateFormat(format);

            strCurrentDate = dateFormat.format(new Date());

            forceFolderExist(strBackupPath + strCurrentDate
                    + strAdditionPath);
            if (!renameFile(strSourcePath + strSourceFile, strBackupPath
                    + strCurrentDate + strAdditionPath + strBackupFile,
                    bReplace))
                throw new Exception("Cannot rename file " + strSourcePath
                        + strSourceFile + " to " + strBackupPath
                        + strCurrentDate + strBackupFile);
            return strBackupPath + strCurrentDate + strBackupFile;
        }
        return "";
    }

    /**
     * Rename file from source to destination
     * 
     * @param strSrc
     *            String
     * @param strDest
     *            String
     * @param deleteIfExist
     *            boolean
     * @return boolean
     * @throws IOException
     * @author Thai Hoang Hiep
     */
    // //////////////////////////////////////////////////////
    public static boolean renameFile(String strSrc, String strDest,
            boolean deleteIfExist) throws IOException {
        File flSrc = new File(strSrc);
        File flDest = new File(strDest);
        if (flSrc.getAbsolutePath().equals(flDest.getAbsolutePath()))
            return false;
        if (flDest.exists()) {
            if (deleteIfExist)
                flDest.delete();
            else
                throw new IOException("File '" + strDest
                        + "' already exist");
        }
        return flSrc.renameTo(flDest);
    }

    /**
     * Rename file from src to des, override if des exist
     * 
     * @param strSrc
     *            source file
     * @param strDest
     *            destination file
     * @return true if succees, otherswise false
     * @author Thai Hoang Hiep
     */
    // //////////////////////////////////////////////////////
    public static boolean renameFile(String strSrc, String strDest) {
        File flSrc = new File(strSrc);
        File flDest = new File(strDest);
        if (flSrc.getAbsolutePath().equals(flDest.getAbsolutePath()))
            return true;
        if (flDest.exists())
            flDest.delete();
        return flSrc.renameTo(flDest);
    }

    /**
     * Close object safely
     * 
     * @param is
     *            InputStream
     */
    // //////////////////////////////////////////////////////
    public static void safeClose(InputStream is) {
        try {
            if (is != null)
                is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Close object safely
     * 
     * @param os
     *            OutputStream
     */
    // //////////////////////////////////////////////////////
    public static void safeClose(OutputStream os) {
        try {
            if (os != null)
                os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Close object safely
     * 
     * @param fl
     *            RandomAccessFile
     */
    // //////////////////////////////////////////////////////
    public static void safeClose(RandomAccessFile fl) {
        try {
            if (fl != null)
                fl.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Delete file
     * 
     * @param strSrc
     *            file to delete
     * @return true if succees, otherswise false
     * @author Thai Hoang Hiep
     */
    // //////////////////////////////////////////////////////
    public static boolean deleteFile(String strSrc) {
        File flSrc = new File(strSrc);
        return flSrc.delete();
    }

    /**
     * Create folder if it's not exist
     * 
     * @param folder
     *            folder to create if it does not exist
     * @throws IOException
     * @author Thai Hoang Hiep
     */
    // //////////////////////////////////////////////////////
    public static void forceFolderExist(String folder) throws IOException {
        File flTemp = new File(folder);
        if (!flTemp.exists()) {
            if (!flTemp.mkdirs())
                throw new IOException("Could not create folder " + folder);
        } else if (!flTemp.isDirectory())
            throw new IOException("A file with name" + folder
                    + " already exist");
    }

    /**
     * 
     * @param strCurrenDir
     *            String
     * @param strFileName
     *            String
     * @return String
     */
    // //////////////////////////////////////////////////////
    public static String getAbsolutePath(String strCurrenDir,
            String strFileName) {
        if (!strFileName.startsWith("/") && !strFileName.startsWith("\\")) {
            if (!strCurrenDir.endsWith("/") && !strCurrenDir.endsWith("\\"))
                return strCurrenDir + "/" + strFileName;
            return strCurrenDir + strFileName;
        }
        return strFileName;
    }
}

Related

  1. backup(String strFileName, int iMaxSize)