Java学习工具类

整理如下工具类

import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.HashMap;

/**
 * 工具类
 * @author legendary
 *
 */
public final class MyUtil {
    private MyUtil(){
        throw new AssertionError();
    }
    /**
     * 读取文件
     * @return 键值Map
     */
    public static HashMap<String, Object> readFile(){
        System.out.println("读成功");
        ObjectInputStream in = null;
        HashMap<String, Object> all = null;

        try {
            in = new ObjectInputStream(new FileInputStream("file.data"));
            all = (HashMap<String, Object>) in.readObject();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return all;
    }
    /**
     * 写文件
     * @param map 写出键值对
     * @throws ClassNotFoundException 可能存在异常
     */
    public static void writeFile(HashMap<String, Object> map) throws ClassNotFoundException{
        ObjectOutputStream out = null;

        try {
            out = new ObjectOutputStream(new FileOutputStream("file.data"));
            out.writeObject(map);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally{
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    /**
     * 短除法求最大公约数
     * @param x 输入的整数 x
     * @param y 输入的整数 y
     * @return 最大公约数
     */
    public static int gcd(int x, int y){
        if (x > y) {
            return gcd(y,x);
        }
        else if (y % x != 0) {
            return gcd(y % x , x);
        }
        else {
            return x;
        }
    }

    /**
     * 求整数 min和 max范围内的一个随机整数
     * @param min 最小范围
     * @param max 最大范围
     * @return 随机值
     */
    public static int random(int min ,int max){
        return (int) (Math.random()*(max - min +1) + min);
    }
    /**
     * 产生一个随机颜色
     * @return 颜色
     */
    public static Color color(){
        int red = (int) (Math.random()*MyUtil.random(0, 250));
        int green = (int) (Math.random()*MyUtil.random(0, 250));
        int blue = (int) (Math.random()*MyUtil.random(0, 250));
        return new Color(red, green, blue);
    }
    /**
     * 计算阶乘 n ! = n * (n-1) * (n - 2)……*2 *1
     * @param n
     * @return  
     * @throws Exception
     */
    public static double factorial(int n) throws TooSmallException,TooBigException{
        if (n < 0 ) {
            throw new TooSmallException("参数值不能小于0");
        }else if (n >170) {
            throw new TooBigException("参数值不能大于170");
        }
        double defult = 1;
        for (int i = 1; i < n; i++) {
            defult *= i;
        }
        return defult;
    }
    /**
     * 拷贝文件
     * @param sourceFile 源文件地址及名字
     * @param targetFile 目标地址及名字
     * @throws IOException 输入输出异常
     */
    public static void fileCopy(String sourceFile, String targetFile) throws IOException{
        InputStream in  = null;
        OutputStream out = null;
        try {
            in = new FileInputStream(sourceFile);
            int indexofSlash = targetFile.lastIndexOf("/");
            if (indexofSlash > 1) {
                File f = new File(targetFile.substring(0, indexofSlash));
                if (!f.exists()) {
                    f.mkdirs();
                }
            }
            out = new FileOutputStream(targetFile);
            byte[] buffer = new byte[1024];
            int b = in.read(buffer);
            while (b != -1) { //文件结束符号
                out.write(buffer, 0, b);
                b = in.read(buffer);
            }
        } catch (IOException e) {
            throw e;
        } finally{
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }
    /**
     * 声明两个新的异常
     * @author legendary
     * 太小、太大
     */
    private static class TooSmallException extends Exception{
        public TooSmallException(String msg){
            super(msg);
        }
    }
    private static class TooBigException extends Exception{
        public TooBigException(String msg){
            super(msg);
        }
    }
}
     /**
     * 二分查找法
     * @param array 有序的数组
     * @param key 待找的元素
     * @return  找到就返回数组下标,没有就返回-1
     */
    public static <T extends Comparable<T>> int binarySearch(T[] array, T key){
        int min = 0;
        int max = array.length -1;
        while (min <=max) {
            int mid = (min + max) >>> 1;
            if (array[mid].compareTo(key) > 0) {
                max = mid - 1;
            } else if (array[mid].compareTo(key) < 0) {
                min = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }
    /**
     * 鸡尾酒排序
     * @param array 等待排序的数组
     */
    public static <T extends Comparable<T>> void sort(T[] array){
        boolean swapped = true;
        for (int i = 1;swapped && i <= array.length -1;) {
            swapped = false;
            switch (i%2) {
            case 0:
                for (int j = 0; j < array.length - i; j++) {
                    if (array[j].compareTo(array[j + 1]) > 0) {
                        T temp = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = temp;
                        swapped = true;
                    }
                }
                break;
            case 1:
                for (int j = array.length -1; j >= i; j--) {
                    if (array[j-1].compareTo(array[j]) > 0) {
                        T temp = array[j - 1];
                        array[j-1] = array[j];
                        array[j] = temp;
                        swapped = true;
                        i++;
                    }
                }
                break;
            }
        }
    }
    /**
     * 冒泡排序
     * @param array 暂时无法排序的数组
     * @param comp 定义零时排序规则
     */
    public static <T> void sort(T[] array,Comparator<T> comp){
        boolean swapped = true;
        for (int i = 1;swapped && i <= array.length -1; i++) {
            swapped = false;
            for (int j = 0; j < array.length - i; j++) {
                if (comp.compare(array[j],array[j+1]) > 0) {
                    T temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                    swapped = true;
                }
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值