Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

import java.text.SimpleDateFormat;

import java.util.Date;

import android.os.Environment;

public class Main {
    public static final SimpleDateFormat newFormat1 = new SimpleDateFormat("dd-MMM-yyyy-HH-mm-ss");

    public static void restoreDb(File currentDB, String restoreDBFileName) {
        try {
            backupDb(currentDB);
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();

            if (sd.canWrite()) {
                File f = new File(sd.getAbsolutePath() + "/AccountManagement");
                if (!f.exists()) {
                    f.mkdir();
                }
                String restoreDBPath = "AccountManagement/" + restoreDBFileName;
                File restoreDB = new File(sd, restoreDBPath);

                if (currentDB.exists()) {
                    FileChannel src = new FileInputStream(restoreDB).getChannel();
                    FileChannel dst = new FileOutputStream(currentDB).getChannel();
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public static void backupDb(File currentDB) {
        try {

            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();

            if (sd.canWrite()) {
                File f = new File(sd.getAbsolutePath() + "/AccountManagement");
                if (!f.exists()) {
                    f.mkdir();
                }
                Date currentDate = new Date();
                String currentDateStr = newFormat1.format(currentDate);

                currentDateStr = currentDateStr.trim();
                System.out.println(
                        "---------------------------------------------------------------" + currentDateStr);
                String backupDBPath = "AccountManagement/backup_" + currentDateStr + ".db";
                File backupDB = new File(sd, backupDBPath);

                if (currentDB.exists()) {
                    FileChannel src = new FileInputStream(currentDB).getChannel();
                    FileChannel dst = new FileOutputStream(backupDB).getChannel();
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}