java常用工具类,持续更新
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StreamUtils;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Collections;
import java.util.List;
public class MyUtil {
public static String readFileFromClasspath(String fileName) throws IOException {
Resource resource = new ClassPathResource(fileName);
try (InputStream inputStream = resource.getInputStream()) {
return StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
}
}
public static String readFileFromClasspath2(String fileName) throws IOException {
File file = ResourceUtils.getFile("classpath:" + fileName);
try (InputStream inputStream = Files.newInputStream(file.toPath())) {
return StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
}
}
public static String readFileFromFilesystem(String fileName) throws IOException {
Resource resource = new FileSystemResource(fileName);
try (InputStream inputStream = resource.getInputStream()) {
return StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
}
}
public static String getProjectLocation() {
String userDir = System.getProperty("user.dir");
return userDir;
}
public static void printSystemProperties() {
System.getProperties().forEach((k, v) -> System.out.println(String.format("%s=%s", k, v)));
}
public static String getCodeExecutePoint() {
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
StackTraceElement stackTraceElement = stackTraceElements[2];
return stackTraceElement.toString();
}
public static String getStackTrackFromException(Exception e) {
try (StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw)){
e.printStackTrace(pw);
return "\r\n" + sw+ "\r\n";
} catch (Exception var3) {
return "getStackTrackFromException error";
}
}
public static String defaultIfBlank(String str,String defaultStr){
return (str==null||str.equals(""))?defaultStr:str;
}
public static String defaultIfNull(String str){
return str==null?"":str;
}
public static List<?> defaultIfNull(List<?> list){
return list==null? Collections.emptyList():list;
}
public static void readImageFromUrl(String inputUrl,String outputPath) {
try {
URL url = new URL(inputUrl);
BufferedImage inputImage = ImageIO.read(url);
BufferedImage outputImage = new BufferedImage(inputImage.getWidth(), inputImage.getHeight(), BufferedImage.TYPE_INT_RGB);
outputImage.createGraphics().drawImage(inputImage, 0, 0, null);
ImageIO.write(outputImage, "png", new File(outputPath));
System.out.println("写入成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
readImageFromUrl("https://pics5.baidu.com/feed/b151f8198618367ae98737f813adabd9b21ce5f0.jpeg","output.png");
} catch (Exception e) {
e.printStackTrace();
}
}