欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java 知识点

程序员文章站 2022-04-24 11:12:09
...
  1. 不清晰的知识点
package work.test;

import com.example.controller.UserController;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author chenxinwei
 */
public class DoubleIntercept {
    private static Logger logger = LoggerFactory.getLogger(UserController.class);

    /**
     * 测试double的精度截取
     */
    @Test
    public void testDoubleIntercept() {
        int length = 4;//小数点后几位
        double value = 3.14d;
        BigDecimal bigDecimal = new BigDecimal(value);
        double result = bigDecimal.setScale(length, BigDecimal.ROUND_HALF_UP).doubleValue();
        logger.info("result:" + result);
    }

    /**
     * Map数据的存取
     */
    @Test
    public void testMap() {
        Map<String, String> hashMap = new HashMap<>();
        for (int i = 0; i < 10; i++) {
            hashMap.put(i + "", i * 10 + "");
        }
        //map能添加null
        hashMap.put(null, "test push null key");
        logger.info("hashMap size:" + hashMap.size());//11

        for (Map.Entry<String, String> entry : hashMap.entrySet()) {//输出的时候先输出 key为null的
            logger.info("key[" + entry.getKey() + "],value[" + entry.getValue() + "]");
        }
    }

    /**
     * 测试byte
     */
    @Test
    public void testByte() throws Exception {
        byte[] bytes = "1".getBytes();

        logger.info("bytes" + bytes.toString());
    }

    /**
     * 测试writeByte
     */
    @Test
    public void testWriteByte() throws Exception {
        //java新增二进制字面量
        int ivar100 = 0b11111111_11111111_11111111_10011100;//-100
        int ivar = 0b10011100;//-100
        logger.info("ivar[" + ivar100 + "]");
        OutputStream outputStream = new FileOutputStream("E:\\test.txt");
        outputStream.write(ivar);
        outputStream.flush();
    }

    /**
     * String compare 字符串截取 与比较
     */
    @Test
    public void StringCompare() {
        String dbfName = "ZL171204.dbf";
        String varyDate = "2017-12-04";
        String dbfVaryDate = varyDate.replace("-", "");
        logger.info("dbfVaryDate[" + dbfVaryDate + "]");
        logger.info("compare[" + dbfName.substring(2, 8) + "]");
        logger.info("dbfVaryDate[" + dbfVaryDate.substring(2) + "]");
        System.out.println(dbfName.substring(2, 8).equals(dbfVaryDate.substring(2)));
    }

    @Test
    public void testLong2String() {
        Long var = 0L;
        logger.info("var to String[" + var + "]");
    }

    @Test
    public void testTryCatch() {
        final String filePath = "E:\\log.txt";
        File file = new File(filePath);
        if (!file.exists()) {
            logger.info("文件不存在");
        }
        try (FileInputStream fileInputStream = new FileInputStream(file);
             InputStreamReader fileInputStreamReader = new InputStreamReader(fileInputStream);
             BufferedReader bufferedReader = new BufferedReader(fileInputStreamReader)) {

            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                if (line.equals("")) {
                    continue;
                }

                logger.info("line:" + line);
            }
        } catch (IOException e) {
            logger.error("获取文件流失败" + e);
        }
    }

    @Test
    public void dateAfter() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(2017,11,13);
        Date varyDay = calendar.getTime();
        Date nextTradeDay = Calendar.getInstance().getTime();
        System.out.println("=============varyDay=================");
        System.out.println(varyDay.toString());
        System.out.println("=============nextTradeDay=================");
        System.out.println(nextTradeDay.toString());
        System.out.println(!varyDay.after(nextTradeDay));
    }

}
  1. 反射
package work.test;

import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

/**
 * Created by IntelliJ IDEA.
 * File: TestRef.java
 * User: leizhimin
 * Date: 2008-1-28 14:48:44
 */
public class TestRef {

    public static void main(String args[]) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Foo foo = new Foo("这个一个Foo对象!");
        Class clazz = foo.getClass();
        Method m1 = clazz.getDeclaredMethod("outInfo");
        Method m2 = clazz.getDeclaredMethod("setMsg", String.class);
        Method m3 = clazz.getDeclaredMethod("getMsg");
        m1.invoke(foo);
        m2.invoke(foo, "重新设置msg信息!");
        String msg = (String) m3.invoke(foo);
        System.out.println(msg);
    }
}

class Foo {
    private String msg;

    public Foo(String msg) {
        this.msg = msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getMsg() {
        return msg;
    }

    public void outInfo() {
        System.out.println("这是测试Java反射的测试类");
    }
}