package com.xypower.common;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FileUtils {




    public static boolean DeleteFilesInPath(String path, long seconds) {
        File pathFile = new File(path);
        if (!pathFile.exists() || !pathFile.isDirectory()) {
            return false;
        }

        long ts = System.currentTimeMillis();
        long diff = 0;
        seconds *= 1000;

        List<File> files = new ArrayList<File>();

        for (File f : pathFile.listFiles()) {
            if (!f.isFile()) {
                continue;
            }
            diff = ts - f.lastModified();
            if (diff > seconds) {
                files.add(f);
            }
        }

        for (File f : files) {
            f.delete();
        }

        return true;
    }

    public static boolean DeleteFilesInPath(String path) {
        File pathFile = new File(path);
        if (!pathFile.exists() || !pathFile.isDirectory()) {
            return false;
        }

        for (File f : pathFile.listFiles()) {
            if (!f.isFile()) {
                continue;
            }

            f.delete();
        }

        return true;
    }

    public static byte[] int2Byte(int intValue) {
        byte[] b = new byte[4];
        for (int i = 0; i < 4; i++) {
            b[i] = (byte) (intValue >> 8 * (3 - i) & 0xFF);
            //System.out.print(Integer.toBinaryString(b[i])+" ");
            //System.out.print((b[i] & 0xFF) + " ");
        }
        return b;
    }

    /**
     * 4位字节数组转换为整型
     * @param b
     * @return
     */
    public static int byte2Int(byte[] b) {
        int intValue = 0;
        for (int i = 0; i < b.length; i++) {
            intValue += (b[i] & 0xFF) << (8 * (3 - i));
        }
        return intValue;
    }


}
