Java相关基础操作(JDK17)

读写文件

package org.example;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileOpt {
  public static void main(String[] args) throws IOException {
    String fileName = "sample.txt";
    String content = "hello file!你好 文件!";
    writeFile(fileName, content, false);
    writeFile(fileName, content, true);
    readFile(fileName);
  }

  private static void readFile(String fileName) throws IOException {
    FileReader fr = new FileReader(fileName);
    BufferedReader br = new BufferedReader(fr);
    try (br; fr) {
      String s;
      while ((s = br.readLine()) != null)
        System.out.println(s);
    }
  }

  private static void writeFile(String fileName, String content, boolean append) throws IOException {
    File file = new File(fileName);
    if (file.exists()) {
      file.createNewFile();
    }
    FileWriter fw = new FileWriter(fileName, append);
    BufferedWriter bw = new BufferedWriter(fw);
    //无需手动close
    try (fw; bw) {
      bw.write(content);
      bw.newLine();
    }
  }
}

List、Map操作

List转Map
Map<Long, A> stateMap = searchStateList.stream().collect(Collectors.toMap(A::getId, A -> A));

多线程

创建线程
package org.example;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadOpt {
  public static void main(String[] args) {
    // 线程池
    ExecutorService threadPool = Executors.newFixedThreadPool(5);
    for (int i = 0; i < 10; i++) {
      threadPool.execute(() -> System.out.println(Thread.currentThread().getName() + " is running"));
    }

    // 匿名内部类
    new Thread(() -> System.out.println("running...")).start();
  }
}

泛型

方法
package org.example;

import org.example.model.A;
import org.example.model.B;

public class GenericOpt {
  public static void main(String[] args) {
    System.out.println(judge(new A()));
    System.out.println(judge(new B()));
    System.out.println(judge(""));
  }

  public static <T> String judge(T obj) {
    if (obj instanceof A) {
      return "A";
    } else if (obj instanceof B) {
      return "B";
    }
    return null;
  }
}

注解

自定义注解

依赖

implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-aop'
package org.example.annotation;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

@Component
@Aspect
public class CustomAspect {

  @Pointcut("@annotation(org.example.annotation.CustomAnno)")
  public void CustomPointCut() {
  }

  @Around("CustomPointCut()")
  public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
    Class<?> aClass = joinPoint.getTarget().getClass();
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    String methodName = "";
    try {
      methodName = aClass.getDeclaredMethod(signature.getName(), signature.getParameterTypes()).getName();
    } catch (NoSuchMethodException e) {
      e.printStackTrace();
    }
    StopWatch stopWatch = new StopWatch(methodName);
    stopWatch.start();
    Object[] args = joinPoint.getArgs();
    Object res;
    try {
      res = joinPoint.proceed(args);
    } finally {
      stopWatch.stop();
    }
    return res;
  }
}

package org.example.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Inherited
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented//是否能随着java文件生成javadoc文档
public @interface CustomAnno {
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值