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 {
    /**
     * Copy a file by using the file streams
     * 
     * @param src     the source file
     * @param dest    the destination file
     * @throws IOException
     */
    public static void copyFileUsingFileStreams(File source, File dest) throws IOException {
        InputStream input = null;
        OutputStream output = null;

        try {
            input = new FileInputStream(source);
            output = new FileOutputStream(dest);

            copyFileUsingStream(input, output);
        } finally {
            input.close();
            output.close();
        }
    }

    private static void copyFileUsingStream(InputStream in, OutputStream out) throws IOException {
        byte[] buf = new byte[1024];
        int read;
        while ((read = in.read(buf)) != -1) {
            out.write(buf, 0, read);
        }
    }
}