FileUtil.java 3.89 KB
package com.sw.laryngoscope.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class FileUtil {

    public static int CopyFile(String fromFile, String toFile) {
        Logger.d(" copyShockFileToSd "+toFile+"--"+fromFile + "length = "+ (new File(fromFile)).length());
        FileChannel input = null;
        FileChannel output = null;
        try {
            input = new FileInputStream(new File(fromFile)).getChannel();
            output = new FileOutputStream(new File(toFile)).getChannel();
            long length = output.transferFrom(input, 0, input.size());
            Logger.e(" copyShockFileToSd length = "+length);
        } catch (Exception e) {
            Logger.w(" error occur while CopySdcardFile", e.toString());
        } finally {
            try {
                input.close();
                output.close();
                return 0;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return -1;
    }

    public static boolean copyfile(String fromFile, String toFile) {
        File src = new File(fromFile), dst = new File(toFile);
        long inSize = src.length();
        long outSize = 0;
        int progress = 0;
        try {
            if(dst.exists()){
                dst.delete();
            }
            dst.createNewFile();
            FileInputStream in = new FileInputStream(src);
            FileOutputStream out = new FileOutputStream(dst);
            int length = -1;
            byte[] buf = new byte[1024 * 8];
            while ((length = in.read(buf)) != -1) {
                out.write(buf, 0, length);
                outSize += length;
                int temp = (int) (((float) outSize) / inSize * 100);
                if (temp != progress) {
                    progress = temp;
                }
            }
            out.flush();
            in.close();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static int copyFileToUsb(String fromDir, String toDir) {
        //要复制的文件目录
        File[] currentFiles;
        File root = new File(fromDir);
        //如同判断SD卡是否存在或者文件是否存在
        //如果不存在则 return出去
        if ( !root.exists() || root.list().length ==0 ) {
            return -1;
        }
        //如果存在则获取当前目录下的全部文件 填充数组
        currentFiles = root.listFiles();
        //目标目录
        File targetDir = new File(toDir);
        //判断目录是否存在
        if ( !targetDir.exists() ) {
            targetDir.mkdirs();
        }
        //遍历要复制该目录下的全部文件
        for( int i= 0; i < currentFiles.length; i++ ) {
            if ( new File(currentFiles[i].getPath()).isDirectory() )
                continue ;
            copyfile(currentFiles[i].getPath(), toDir + currentFiles[i].getName());
        }
        return 0;
    }

    public static void clearLogFile(String rootDir) {
        //要复制的文件目录
        File[] currentFiles;
        File root = new File(rootDir);
        if ( !root.exists() || root.list().length == 0 ) {
            return ;
        }
        currentFiles = root.listFiles();
        /*for( int i = 0; i < currentFiles.length; i++ ) {
            Logger.d(" currentFiles getPath " + currentFiles[i].getPath());
        }*/
        if ( currentFiles.length <= 30 ) {
            return ;
        }
        for( int i = 0; i < currentFiles.length - 30; i++ ) {
            new File(currentFiles[i].getPath()).delete();
        }
    }
 
}