잡동사니

항상 정리하기·2022년 6월 11일
0

Utils.java

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;

public class Utils {

	public static void printCurrentPath() {
		String currentDir = System.getProperty("user.dir");
		System.out.println("Current dir using System:" + currentDir);
	}

	public static void sleep(long ms) {
		try {
			Thread.sleep(ms);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

/*
	public static String toJson(Object o) {
		return new Gson().toJson(o);
	}

	public static <T> T fromJson(String s, Class<T> cls) {
		return new Gson().fromJson(s, cls);
	}
*/
	public static String getUuid() {
		return UUID.randomUUID().toString();
	}

	// run external application
	public static void execute(String... args) throws IOException {
		Process process = new ProcessBuilder(args).start();
		InputStream is = process.getInputStream();
		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(isr);
		String line;

		System.out.printf("Output of running %s is:", Arrays.toString(args));

		while ((line = br.readLine()) != null) {
			System.out.println(line);
		}
	}

	public static List<String> getFileList(String path) throws IOException {
		return Files.list(new File(path).toPath()).map(p -> p.toString()).toList();
	}

	public static List<String> getFileListRecursive(String path) throws IOException {
		List<String> files = new ArrayList<>();
		try (Stream<Path> paths = Files.walk(Paths.get(path))) {
			paths.filter(Files::isRegularFile).forEach(p -> files.add(p.toString()));
		}
		return files;
	}

	public static String encodeBase64(String s) throws UnsupportedEncodingException {
		return Base64.getEncoder().encodeToString(s.getBytes(StandardCharsets.UTF_8));
	}

	public static String decodeBase64(String s) {
		return new String(Base64.getDecoder().decode(s), StandardCharsets.UTF_8);
	}

	public String encrypt(String text) throws NoSuchAlgorithmException {
		MessageDigest md = MessageDigest.getInstance("SHA-256");
		md.update(text.getBytes());

		return bytesToHex(md.digest());
	}

	private String bytesToHex(byte[] bytes) {
		StringBuilder builder = new StringBuilder();
		for (byte b : bytes) {
			builder.append(String.format("%02x", b));
		}
		return builder.toString();
	}

	public static void writeFile(String content) throws IOException {
		try (BufferedWriter writer = new BufferedWriter(new FileWriter("./OUTPUT/REPORT.TXT"))) {
			writer.write(content);
		}
	}

	public static List<String> readFile() {
		List<String> content = new ArrayList<>();
		try (BufferedReader reader = new BufferedReader(new FileReader("./INPUT/MONITORING.TXT"))) {
			while (true) {
				String line = reader.readLine();
				if (line == null) {
					break;
				}

				content.add(line);
			}
			return content;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void copyFile(String src, String dst) {
		try (InputStream inputStream = new FileInputStream(src);
				OutputStream outputStream = new FileOutputStream(dst);) {
			int byteRead = -1;
			int BUF_LEN = 4096;
			byte[] bytes = new byte[BUF_LEN];
			while ((byteRead = inputStream.read(bytes)) != -1) {
				outputStream.write(bytes, 0, byteRead);
			}

		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public static String getInput() throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		String line = reader.readLine();
		return line;
	}


	public static long timeDiff(String t1, String t2) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

		Date d1 = sdf.parse(t1);
		Date d2 = sdf.parse(t2);
		
		long diff = d1.getTime() - d2.getTime();
		
		return diff / 1000;
	}

	

	public static void example_sort() {
		List<String> strList = new ArrayList<>();
		Collections.sort(strList);
		Collections.sort(strList, Collections.reverseOrder());
		Collections.sort(strList, (m1, m2) -> m1.compareTo(m2));

		strList.sort((m1, m2) -> m1.length() - m2.length());

		int[] ar = {};
		Arrays.sort(ar);

		List<String> strList2 = Arrays.asList("1", "2");
		Collections.sort(strList2);
	}
	
	public static void example_time() throws ParseException {
		
		LocalDateTime now = LocalDateTime.now();
		String nowS = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
		
		long ct = System.currentTimeMillis();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		String sdfS = sdf.format(ct);
		
		String sTime = "2022-12-12 12:00:00";
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		Date dt = format.parse(sTime);
		
		LocalDateTime ldt = dt.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
	}

}

ResponseBody.java

public class ResponseBody {
    public String result = "ok";
    
    public ResponseBody() {
        
    }
    
    public String toString() {
        return new Gson().toJson(this);
    }

}

ThreadInClass.java

public class ThreadInClass {
    public void run() throws Exception {
        Thread th = new Thread(() -> {
            while(true) {
                Utils.sleep(1000);
                
                // todo
            }
        });
        
        th.start();
    }
}

이클립스에서 input 데이터 파일 설정

자동완성 설정
https://devlimk1.tistory.com/9

이클립스 단축키

functionshortcut
runctrl + F11
debugF11
step infoF5
step overF6
step returnF7
resumeF8
profile
늦은 것 같지만 이제부터라도 차근차근 하나씩 정리하기

0개의 댓글