Blame view

src/main/java/com/batch/util/FileUtil.java 1.42 KB
8dc487b1   함상기   Init Version - 20...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  package com.batch.util;

  

  import java.io.BufferedReader;

  import java.io.InputStreamReader;

  

  import org.springframework.core.io.ClassPathResource;

  

  import com.google.gson.Gson;

  import com.google.gson.JsonObject;

  import com.google.gson.JsonParser;

  

  public class FileUtil {

  

  	public static StringBuffer readFileToString(String resourceName) {

          StringBuffer sb = new StringBuffer();

          try {

  			ClassPathResource resource = new ClassPathResource(resourceName);

  	        BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()));

  	

  	        // br.readLine() 이 null 인지 검사할 때 한번 사용되므로 String 에 먼저 저장해둬야한다.

  	        String s = "";

              while((s = br.readLine()) != null){

              	sb.append(s);

              }		

          } catch (Exception e) {

          	sb.append(e.getStackTrace());

          }

          return sb;

  	}

  

  	public static JsonObject strToJsonObj(String jsonString) {

  		JsonObject jsonObject = JsonParser.parseString(jsonString).getAsJsonObject();

  		return jsonObject;

  	}

  	

  	public static Object strToObj(String jsonString, Class<?> anyClass) {

  		Gson gson = new Gson();

  		Object convertedObject = gson.fromJson(jsonString, anyClass);

  		return convertedObject;

  	}

  

  	public static String objToStr(Object obj) {

  		Gson gson = new Gson();

  		String stringObject = gson.toJson(obj);

  		return stringObject;

  	}

  	

  }