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 java.io.InputStream;
import java.io.OutputStream;

public class Main {
    public static final int BUFFER_SIZE = 8192;

    /**
     * Creates a copy of the file
     * @param in Source file
     * @param out Destination file
     * @throws IOException if the operation fails.
     * 
     */
    public static void copyFile(File in, File out) throws IOException {
        InputStream in_stream = new FileInputStream(in);
        OutputStream out_stream = new FileOutputStream(out);
        copyStream(in_stream, out_stream);
        in_stream.close();
        out_stream.close();
    }

    /**
     * Copies inputStream to outputStream in a somewhat buffered way
     * @param in Input stream
     * @param out Output stream
     * @throws IOException if the operation fails
     */
    public static void copyStream(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[BUFFER_SIZE];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
    }
}