天天看点

JAVA基础库的使用点滴

Idea中双击SHIFT可以搜索[Eclipse中也有],这个很重要,可以找到当前的项目中可以引擎的已有的类,不要再次自己发明轮子

各种Util先在基础库和开源库中找

guava:com.google.common.io.BaseEncoding

commons-codec:org.apache.commons.codec.binary.Base64

各种摘要等算法都有了

[WARNING] /opt/work/2014/work/centos2017/webman/webman/src/main/java/cn/csbit/util/License.java:[370,41] sun.misc.BASE64Encoder是内部专用 API, 可能会在未来发行版中删除

[WARNING] /opt/work/2014/work/centos2017/webman/webman/src/main/java/cn/csbit/core/report/vo/ReportVo.java

commons-lang

org.apache.commons.lang.SerializationUtils

org.apache.commons.lang.time

DateFormatUtils DateUtils

cn.csbit.core.util.DateUtil

JAVA基础库的使用点滴

org.apache.commons.codec.binary

org.apache.commons.lang StringUtils

JAVA基础库的使用点滴

  JDK新的版本才加的方便文件操作的类已经都有了

commons-io

org.apache.commons.io FilenameUtils 文件名的各种处理

FileSystemUtils 磁盘空间的状况

JAVA基础库的使用点滴

Ints Longs Shorts UnsignedInts

com.google.common.primitives

@GwtIncompatible("doesn\'t work")

public static byte[] toByteArray(int value) {

return new byte[]{(byte)(value >> 24), (byte)(value >> 16), (byte)(value >> 8), (byte)value};

}

public static int fromByteArray(byte[] bytes) {

Preconditions.checkArgument(bytes.length >= 4, "array too small: %s < %s", new Object[]{Integer.valueOf(bytes.length), Integer.valueOf(4)});

return fromBytes(bytes[0], bytes[1], bytes[2], bytes[3]);

public static byte[] shortToBytes(short number) {

int temp = number;

byte[] b = new byte[2];

for (int i = 0; i < b.length; i++) {

b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位

temp = temp >> 8; // 向右移8位

return b;

byte[] array = new byte[4];

for (int i = 0; i < 4; i++) {

array[i] = (byte) ((value >> (8 * i)) & 0XFF);

return array;

NumberUtil common-lang也有类似

      #include <arpa/inet.h>

       uint32_t htonl(uint32_t hostlong);

       uint16_t htons(uint16_t hostshort);

       uint32_t ntohl(uint32_t netlong);

       uint16_t ntohs(uint16_t netshort);

ByteBuffer b = ByteBuffer.allocate(4);

b.order(ByteBuffer.LITTEN_ENDIAN);

b.putIn(100);

b.array();//可以获取字节数组

com.google.common.net.InetAddresses