copy Database File - Android java.io

Android examples for java.io:File Copy

Description

copy Database File

Demo Code

import android.os.Environment;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;

public class Main{

    public static void copyDatabase(File fromFile, File toFile) {
        FileInputStream fis;/*from  ww w.  ja v  a 2  s  . c o m*/
        OutputStream output;
        try {
            fis = new FileInputStream(fromFile);

            // Open the empty db as the output stream
            output = new FileOutputStream(toFile);

            // Transfer bytes from the inputfile to the outputfile
            byte[] buffer = new byte[1024];
            int length;
            try {
                while ((length = fis.read(buffer)) > 0) {
                    output.write(buffer, 0, length);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println("Error IOException : " + e.getMessage());
            }

            // Close the streams
            try {
                output.flush();
                output.close();
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println("Error close IOException : "
                        + e.getMessage());
            }

        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            System.out.println("Error FileNotFoundException : "
                    + e1.getMessage());
        }
    }

}

Related Tutorials