Android File Copy copyFileLazy(String source, String destination)

Here you can find the source of copyFileLazy(String source, String destination)

Description

copy File Lazy

License

Apache License

Declaration

public static void copyFileLazy(String source, String destination)
            throws IOException 

Method Source Code

/*/*from   www. j av a  2s . com*/
 * Copyright 2004-2005 Germinus XXI, Liferay LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * This is a derived work from source code taken from Liferay.
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import org.apache.commons.lang.StringUtils;

public class Main{
    public static void copyFileLazy(String source, String destination)
            throws IOException {

        String oldContent = null;
        try {
            oldContent = FileUtil.read(source);
        } catch (FileNotFoundException fnfe) {
            return;
        }

        String newContent = null;
        try {
            newContent = FileUtil.read(destination);
        } catch (FileNotFoundException fnfe) {
        }

        if (oldContent == null || !oldContent.equals(newContent)) {
            FileUtil.copyFile(source, destination);
        }
    }
    public static String read(String fileName) throws IOException {
        return read(new File(fileName));
    }
    public static String read(File file) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(file));

        StringBuffer sb = new StringBuffer();
        String line = null;

        while ((line = br.readLine()) != null) {
            sb.append(line).append('\n');
        }

        br.close();

        return sb.toString().trim();
    }
    public static void copyFile(String sourceFileName,
            String destinationFileName) {

        copyFile(new File(sourceFileName), new File(destinationFileName));
    }
    public static void copyFile(File source, File destination) {
        if (!source.exists()) {
            return;
        }

        if ((destination.getParentFile() != null)
                && (!destination.getParentFile().exists())) {

            destination.getParentFile().mkdirs();
        }

        try {
            FileChannel srcChannel = new FileInputStream(source)
                    .getChannel();
            FileChannel dstChannel = new FileOutputStream(destination)
                    .getChannel();

            dstChannel.transferFrom(srcChannel, 0, srcChannel.size());

            srcChannel.close();
            dstChannel.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    public static boolean exists(String fileName) {
        File file = new File(fileName);

        return file.exists();
    }
    public static void mkdirs(String pathName) {
        File file = new File(pathName);
        file.mkdirs();
    }
}

Related

  1. copyFile(String sourcePath, String destPath)
  2. copyFile(String sourcePathName, String destPathName)
  3. copyFile(String srcFile, String destFile)
  4. copyFile(String strSrc, String strDest)
  5. copyFileByCommand(String src, String dst, boolean isMove)
  6. copyFileToFile(File file, File outFile)
  7. copyFiles(File src, File dst)
  8. copyImgFile(String sourcePath, String fileId)
  9. copyResource(Class cls, String strResSource, String strFile)