Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.util.Log;

public class Main {
    public static boolean copyFile(File oldPlace, File newPlace) {
        return moveFile(oldPlace, newPlace, false);
    }

    public static boolean moveFile(File oldPlace, File newPlace) {
        return moveFile(oldPlace, newPlace, true);
    }

    private static boolean moveFile(File oldPlace, File newPlace, boolean removeOld) {
        boolean removeNewFile = true;
        Log.i("cr3", "Moving file " + oldPlace.getAbsolutePath() + " to " + newPlace.getAbsolutePath());
        if (!oldPlace.exists()) {
            Log.i("cr3", "File " + oldPlace.getAbsolutePath() + " does not exist!");
            return false;
        }
        FileOutputStream os = null;
        FileInputStream is = null;
        try {
            if (!newPlace.createNewFile())
                return false; // cannot create file
            os = new FileOutputStream(newPlace);
            is = new FileInputStream(oldPlace);
            byte[] buf = new byte[0x10000];
            for (;;) {
                int bytesRead = is.read(buf);
                if (bytesRead <= 0)
                    break;
                os.write(buf, 0, bytesRead);
            }
            removeNewFile = false;
            oldPlace.delete();
            return true;
        } catch (IOException e) {
            return false;
        } finally {
            try {
                if (os != null)
                    os.close();
            } catch (IOException ee) {
                // ignore
            }
            try {
                if (is != null)
                    is.close();
            } catch (IOException ee) {
                // ignore
            }
            if (removeNewFile)
                newPlace.delete();
        }
    }
}