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, ensuring the file is written to the disk
     * @param in Source file
     * @param out Destination file
     * @throws IOException if the operation fails
     */
    public static void copyFileSync(File in, File out) throws IOException {
        FileInputStream inStream = new FileInputStream(in);
        FileOutputStream outStream = new FileOutputStream(out);
        try {
            copyStream(inStream, outStream);
        } finally {
            inStream.close();
            outStream.flush();
            outStream.getFD().sync();
            outStream.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);
        }
    }
}

d