@Data
public class BasicTest {
/**
* 参数检验
*
* @param a
*/
@Test
void testArg(String a) {
if (a == null) {
throw new NullPointerException("参数a为空");
}
if (a == "") {
throw new IllegalArgumentException("参数异常");
}
}
/**
* 二进制和十进制转换
*/
@Test
void testBinaryString() {
int i = 100;
String s = Integer.toBinaryString(i);
System.out.println(s);
System.out.println(Integer.parseInt(s, 2));
}
/**
* 连续两次异或还是自己
*/
@Test
void testTwoAction() {
int i = (6 ^ 3) ^ 3;
System.out.println(i);
}
/**
* 00000110
* & 00000011
* ------------------
* 00000010
*/
@Test
public void test与(){
int a=6&3;
System.out.println(a); //2
System.out.println(Integer.toBinaryString(a)); //10
}
/**
* 00000110
* | 00000011
* ------------------
* 00000111
*/
@Test
public void test或(){
int a=6|3;
System.out.println(a); //7
System.out.println(Integer.toBinaryString(a)); //111
}
/** 相同为false,不同为true
* 00000110
* ^ 00000011
* ------------------
* 00000101
*/
@Test
public void test异或(){
int a=6^3;
System.out.println(a); //5
System.out.println(Integer.toBinaryString(a)); //101
}
/**
* 取反的规则
*
* 先取反 +6变-6
* -7变+7
* 然后都减-1
*/
@Test
public void test取反(){
System.out.println(~6); //-7
System.out.println(~-7); //6
}
/**
* -6在计算机的表示原理
*
* 00000110 先得到+6的2进制表示
* 11111001 取反
* 11111010 再加1
*
* ----------------------
*
* 11111001 先-1
* 00000110 取反后,得到+6
* 再加上-号,那么就是-6
*/
@Test
public void test负数表示(){
String s = Integer.toBinaryString(-6);
System.out.println(s);
}
/**
* 2*3
*/
@Test
public void test移位运算(){
System.out.println(2<<3); //16 2*(2^3)
System.out.println(16>>3); //2 16/(2^3)
System.out.println(-16>>3); //-2
}
@Test
public void test不引用三方变量对两个数进行交换(){
int a=5;
int b=10;
a=a^b;
b=a^b; //(a^b)^b=a
a=a^b; //(a^b)^a=(b^a)^a=b
System.out.printf("%d,%d",a,b);
}
@Test
void testEncrypt() throws IOException {
ClassPathResource classPathResource = new ClassPathResource("1.png");
String path = classPathResource.getFile().getPath();
System.out.println(path);
FileInputStream fileInputStream = new FileInputStream(path);
FileOutputStream fileOutputStream = new FileOutputStream(path.replace("1.png","2.png"));
int content=-1;
while ((content=fileInputStream.read())!=-1){
fileOutputStream.write(content^5);
}
fileOutputStream.close();
fileInputStream.close();
System.out.println("encrypt finished");
}
@Test
void testDecrypt() throws IOException {
ClassPathResource classPathResource = new ClassPathResource("2.png");
String path = classPathResource.getFile().getPath();
System.out.println(path);
FileInputStream fileInputStream = new FileInputStream(path);
FileOutputStream fileOutputStream = new FileOutputStream(path.replace("2.png","3.png"));
int content=-1;
while ((content=fileInputStream.read())!=-1){
fileOutputStream.write(content^5);
}
fileOutputStream.close();
fileInputStream.close();
System.out.println("decrypt finished");
}
@Test
void testBase64() throws UnsupportedEncodingException {
String a="hello";
byte[] encode = Base64.getEncoder().encode(a.getBytes(StandardCharsets.UTF_8));
byte[] decode = Base64.getDecoder().decode(encode);
String s = new String(decode, StandardCharsets.UTF_8);
System.out.println(s);
}
@Test
void testDate() {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = now.format(dateTimeFormatter);
System.out.println(format);
long l = System.currentTimeMillis();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format1 = simpleDateFormat.format(l);
System.out.println(format1);
}
@Test
void testRandom(){
char[] array={'1','2','3','4','5','6','你','中','我','国'};
Random random = new Random();
StringBuilder stringBuilder = new StringBuilder();
for(int i=0;i<4;i++){
int i1 = random.nextInt(array.length);
stringBuilder.append(array[i1]);
}
System.out.println(stringBuilder.toString());
}
@Test
void testRuntime() throws Exception {
Runtime runtime = Runtime.getRuntime();
System.out.println("可用内存:"+runtime.freeMemory());
System.out.println("获取JVM最大使用内存"+runtime.maxMemory());
System.out.println("获取JVM只能使用内存"+runtime.totalMemory());
Process notepad = runtime.exec("notepad");
Thread.sleep(3000);
notepad.destroy();
}
@Test
void runShello() {
String pathOrCommand="";
List<String> result = new ArrayList<>();
try {
// 执行脚本
Process ps = Runtime.getRuntime().exec(pathOrCommand);
int exitValue = ps.waitFor();
if (0 != exitValue) {
System.out.println("call shell failed. error code is :" + exitValue);
}
// 只能接收脚本echo打印的数据,并且是echo打印的最后一次数据
BufferedInputStream in = new BufferedInputStream(ps.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = br.readLine()) != null) {
System.out.println("脚本返回的数据如下: " + line);
result.add(line);
}
in.close();
br.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(result.toString());
}
@Test
public void testArrayCopy(){
int[] src={10,12,14,16,19};
int[] dest=new int[4];
System.arraycopy(src, 0, dest, 2, 2);
System.out.println(Arrays.toString(dest)); //[0,0,10,12]
}
@Test
void testSystemEnv() {
/* Map<String, String> getenv = System.getenv();
for (Map.Entry<String, String> stringStringEntry : getenv.entrySet()) {
System.out.println(stringStringEntry.getKey()+":"+stringStringEntry.getValue());
}*/
String java_home = System.getenv("JAVA_HOME");
System.out.println("java_home:"+java_home);
Properties properties = System.getProperties();
properties.list(System.out);
}
/**
* substring包头不包尾
* endIndex实际为endIndex+1;
*/
@Test
public void testStringSubstring() {
String a="hello,china";
System.out.println(a.substring(0,5)); //hello
System.out.println(a.substring(1,5)); //ello,所谓的endIndex实际上是长度
}
/**
*
* String() 创建一个空内容 的字符串对象。
String(byte[] bytes) 使用一个字节数组构建一个字符串对象
String(byte[] bytes, int offset, int length)
bytes : 要解码的数组
offset: 指定从数组中那个索引值开始解码。
length: 要解码多个元素。
String(char[] value) 使用一个字符数组构建一个字符串。
String(char[] value, int offset, int count) 使用一个字符数组构建一个字符串, 指定开始的索引值,与使用字符个数。
String(int[] codePoints,int offset,int count)
String(String original)
*/
@Test
public void testStringConstructor() {
String str = null;
String src="你好中国";
//转bytes()
byte[] bytes = src.getBytes();
str=new String(bytes,0,2);
System.out.println(str); //你
//转charArray()
char[] charArray = src.toCharArray();
str=new String(charArray,1,3);
System.out.println(str); //好中国
}