Commit dec8d909d2da38a44b78d9e6587aeaba4b6bedbd

Authored by 함상기
1 parent 31cbf4da

2024-04-02 이전변경파일 복구

src/main/java/com/batch/controller/JobController.java
... ... @@ -17,12 +17,10 @@ import org.springframework.web.bind.annotation.RestController;
17 17 import com.batch.config.MatchingSetup;
18 18 import com.batch.config.MatchingSetup.Matching;
19 19 import com.batch.mapper.primary.MatchingInnerDelingMapper;
20   -import com.batch.mapper.secondary.OracleMapper;
21 20 import com.batch.service.JobService;
22 21 import com.batch.util.FileUtil;
23 22 import com.google.gson.JsonObject;
24 23  
25   -import com.batch.controller.JobController;
26 24 import lombok.extern.slf4j.Slf4j;
27 25  
28 26 @RestController
... ...
src/main/java/com/batch/util/FileUtil.java
1 1 package com.batch.util;
2 2  
3 3 import java.io.BufferedReader;
  4 +import java.io.IOException;
4 5 import java.io.InputStreamReader;
  6 +import java.io.RandomAccessFile;
5 7  
6 8 import org.springframework.core.io.ClassPathResource;
7 9  
... ... @@ -12,27 +14,85 @@ import com.google.gson.JsonParser;
12 14 public class FileUtil {
13 15  
14 16 public static StringBuffer readFileToString(String resourceName) {
15   - StringBuffer sb = new StringBuffer();
16   - try {
  17 + StringBuffer sb = new StringBuffer();
  18 + try {
17 19 ClassPathResource resource = new ClassPathResource(resourceName);
18   - BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()));
19   -
20   - // br.readLine() 이 null 인지 검사할 때 한번 사용되므로 String 에 먼저 저장해둬야한다.
21   - String s = "";
22   - while((s = br.readLine()) != null){
23   - sb.append(s);
24   - }
25   - } catch (Exception e) {
26   - sb.append(e.getStackTrace());
27   - }
28   - return sb;
  20 + BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()));
  21 +
  22 + // br.readLine() 이 null 인지 검사할 때 한번 사용되므로 String 에 먼저 저장해둬야한다.
  23 + String s = "";
  24 + while ((s = br.readLine()) != null) {
  25 + sb.append(s);
  26 + }
  27 + } catch (Exception e) {
  28 + sb.append(e.getStackTrace());
  29 + }
  30 + return sb;
  31 + }
  32 +
  33 + /**
  34 + * 파일 마지막 부터 lineCnt행 읽기
  35 + * @param resourceName
  36 + * @param lineCnt
  37 + * @return
  38 + */
  39 + public static StringBuffer readFileLastLines(String resourceName, int lineCnt, String fEnc, String toEnc) {
  40 + StringBuffer sb = new StringBuffer();
  41 +
  42 +// lineCnt = 50; // test case
  43 +// resourceName = "D://logs/localhost_access_log.2019-03-26.txt"; // test case
  44 +
  45 + if(resourceName == null || resourceName.isEmpty()) {
  46 + sb.append("파일 경로가 입력되지 않았습니다.");
  47 + }else if(lineCnt < 1) {
  48 + sb.append("라인갯수가 입력되지 않았습니다.");
  49 + }else {
  50 + RandomAccessFile rf = null;
  51 +
  52 + try {
  53 +
  54 + rf = new RandomAccessFile(resourceName, "r");
  55 + long len = rf.length();
  56 + for (long i = len - 1; i >= 0; i--) {
  57 + rf.seek(i);
  58 + char c = (char) rf.read();
  59 + if (c == '\n') {
  60 + lineCnt--;
  61 + if(lineCnt < 0) {
  62 + break;
  63 + }
  64 + }
  65 +
  66 + sb.insert(0, c);
  67 + }
  68 +
  69 + /*** 필요 시 파일 인코딩. utf-8 **/
  70 + if(fEnc != null && !fEnc.isBlank() && toEnc != null && !toEnc.isBlank()) {
  71 + sb = new StringBuffer(new String((sb.toString()).getBytes(fEnc), toEnc));
  72 + }
  73 +
  74 + } catch (Exception e) {
  75 +// e.printStackTrace();
  76 + sb = new StringBuffer("유효한 파일경로가 아닙니다.("+resourceName+")");
  77 + } finally {
  78 + if (rf != null) {
  79 + try {
  80 + rf.close();
  81 + } catch (IOException e) {
  82 +// e.printStackTrace();
  83 + sb = new StringBuffer("유효한 파일경로가 아닙니다.("+resourceName+")");
  84 + }
  85 + }
  86 + }
  87 + }
  88 + return sb;
29 89 }
30 90  
31 91 public static JsonObject strToJsonObj(String jsonString) {
32 92 JsonObject jsonObject = JsonParser.parseString(jsonString).getAsJsonObject();
33 93 return jsonObject;
34 94 }
35   -
  95 +
36 96 public static Object strToObj(String jsonString, Class<?> anyClass) {
37 97 Gson gson = new Gson();
38 98 Object convertedObject = gson.fromJson(jsonString, anyClass);
... ... @@ -44,5 +104,5 @@ public class FileUtil {
44 104 String stringObject = gson.toJson(obj);
45 105 return stringObject;
46 106 }
47   -
  107 +
48 108 }
... ...
src/main/java/com/batch/util/StatisticsUtil.java
... ... @@ -25,6 +25,16 @@ public class StatisticsUtil {
25 25 // log.info("resultAll=" + resultAll.toString());
26 26 // }
27 27  
  28 + private static long mtch_num = 0;
  29 +
  30 + public static long getMtchNum() {
  31 + return ++mtch_num;
  32 + }
  33 +
  34 + public static void initMtchNum() {
  35 + mtch_num = 0;
  36 + }
  37 +
28 38 /**
29 39 * 조합 구하기
30 40 *
... ...