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;

public class Main {
    /**
     * Copy file from oldPath to newPath
     *
     * @param oldPath
     * @param newPath
     */
    public static void copyFile(String oldPath, String newPath) {
        try {
            int byteread = 0;
            File oldfile = new File(oldPath);
            File newfile = new File(newPath);
            if (newfile.exists()) {
                newfile.delete();
            }
            newfile.createNewFile();
            if (oldfile.exists()) {
                FileInputStream inStream = new FileInputStream(oldPath);
                FileOutputStream outStream = new FileOutputStream(newPath);
                byte[] buffer = new byte[1024];
                while ((byteread = inStream.read(buffer)) > 0) {
                    outStream.write(buffer, 0, byteread);
                }
                inStream.close();
                outStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}