Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 *
 *  *
 *  **    Copyright 2015, The LimeIME Open Source Project
 *  **
 *  **    Project Url: http://github.com/lime-ime/limeime/
 *  **                 http://android.toload.net/
 *  **
 *  **    This program is free software: you can redistribute it and/or modify
 *  **    it under the terms of the GNU General Public License as published by
 *  **    the Free Software Foundation, either version 3 of the License, or
 *  **    (at your option) any later version.
 *  *
 *  **    This program is distributed in the hope that it will be useful,
 *  **    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  **    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  **    GNU General Public License for more details.
 *  *
 *  **    You should have received a copy of the GNU General Public License
 *  **    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  *
 *
 */

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

import java.io.FileOutputStream;

import java.io.InputStream;

public class Main {
    public static boolean copyFile(String sourceFilePath, String targetFilePath, Boolean overWrite) {
        File sourceFile = isFileExist(sourceFilePath);
        if (sourceFilePath == null || sourceFile == null || targetFilePath == null)
            return false;
        File targetFile = isFileExist(targetFilePath);
        if (targetFile != null && !overWrite)
            return false;
        if (targetFile == null)
            targetFile = new File(targetFilePath);
        try {
            FileInputStream inStream = new FileInputStream(sourceFile);
            FileOutputStream outSteram = new FileOutputStream(targetFile);
            copyRAWFile(inStream, outSteram);
            return true;
        } catch (Exception ignored) {
            return false;
        }

    }

    public static File isFileExist(String filepath) {

        File mfile = new File(filepath);
        if (mfile.exists())
            return mfile;
        else
            return null;
    }

    public static void copyRAWFile(InputStream inStream, File newfile) {
        try {
            FileOutputStream fs = new FileOutputStream(newfile);
            copyRAWFile(inStream, fs);
            fs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void copyRAWFile(InputStream inStream, FileOutputStream outStream) {
        try {
            int bytesum = 0, byteread = 0;

            byte[] buffer = new byte[102400]; //100k buffer
            while ((byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread;
                System.out.println(bytesum);
                outStream.write(buffer, 0, byteread);
            }
            inStream.close();
            outStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}