JAVA基础库的使用点滴

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

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

Base64编码

guava:com.google.common.io.BaseEncoding算法

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

各类摘要等算法都有了centos

[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.javaui

日期

commons-langgoogle

org.apache.commons.lang.SerializationUtils编码

org.apache.commons.lang.timespa

DateFormatUtils DateUtils

cn.csbit.core.util.DateUtil

字符串处理

wpsB698.tmp

org.apache.commons.codec.binary

org.apache.commons.lang StringUtils

wpsB6A9.tmp

Files

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

commons-io

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

FileSystemUtils 磁盘空间的情况

wpsB6AA.tmp

数字转换

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};

}

@GwtIncompatible("doesn\'t work")

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;

}

public static byte[] toByteArray(int value) {

byte[] array = new byte[4];

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

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

}

return array;

}

NumberUtil common-lang也有相似

IP地址

      #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

相关文章
相关标签/搜索