Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    public static boolean copyFile(File src, File dst) {
        InputStream is = null;
        OutputStream os = null;
        byte[] buffer = new byte[1024];

        try {
            is = new FileInputStream(src);
            os = new FileOutputStream(dst);

            while (true) {
                int len = is.read(buffer);
                if (len <= 0) {
                    break;
                }

                os.write(buffer, 0, len);
            }

            os.flush();
            return true;

        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    // ignore
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }

    }
}