반응형

 

 

resoruces에 위치한 data.json을 읽어야 한다.

 

 

 

{
  "hello": "123"
}

data.json의 내용은 다음과 같다.

 

 

 

@SpringBootApplication
public class RestApIsApplication {

    public static void main(String[] args) throws IOException {
        SpringApplication.run(RestApIsApplication.class, args);

        ClassPathResource resource = new ClassPathResource("data.json");
        BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()));

        // br.readLine() 이 null 인지 검사할 때 한번 사용되므로 String 에 먼저 저장해둬야한다.
        String s = "";
        while((s = br.readLine()) != null){
            System.out.println(s);

        }
    }
}

스프링에서 resources 파일을 위해 사용되는 ClassPathResource를 사용하면 파일 경로를 src/main/resoruces/부터 바로 읽을 수 있다.

 

 

 

 

File file = new File("src/main/resources/data.json");

ClassPathResource를 안쓰고 경로를 전부 적어서 읽어도 되지만, 경로가 달라질 가능성이 있으므로 ClassPathResource 권장

 

 

 

 

사용법

ClassPathResoruce의 getInputStream() 메서드를 사용하여 InputStream으로 변환한다.

InputStream를 문자를 위한 Reader로 바꾸는 InputStreamReader로 한번 더 감싼다.

InputStreamReader를 성능 향상을 위한 보조스트림 BufferedReader로 한번 더 감싼다.

 

BufferedReader는 Line 단위로 읽는다.

 

출력을 할 때는 while(br.readLine() != null)을 하게 되면, 버퍼의 내용을 한번 소모하므로 String에 저장 대입 후 null인지 검사해야 한다.

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

 

 

 


String s = "";
StringBuilder sb = new StringBuilder();
while((s = br.readLine()) != null){
    sb.append(s);
}
ObjectMapper om = new ObjectMapper();
JsonNode jsonNode = om.readTree(sb.toString());
System.out.println(jsonNode);

 Json으로 사용할 경우

 

 

 

@SpringBootApplication
public class RestApIsApplication {

    public static void main(String[] args) throws IOException {
        SpringApplication.run(RestApIsApplication.class, args);

        ClassPathResource resource = new ClassPathResource("data.json");

        File file = new File("src/main/resources/data.json");
        File copyFile = new File("src/main/resources/data1.json");

        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(copyFile)));

        int size = 0;
        char[] arr = new char[1024];
        while((size = br.read(arr)) != -1){
            bw.write(arr, 0, size);
        }
        
        br.close();
        bw.flush();
        bw.close();
    }
}

파일을 복사할 경우

 

 

 

 

@SpringBootApplication
public class RestApIsApplication {

    public static void main(String[] args) throws IOException {
        SpringApplication.run(RestApIsApplication.class, args);

        ClassPathResource resource = new ClassPathResource("data.json");
        ClassPathResource resourceCopy = new ClassPathResource("data1.json");
        File file = new File(resourceCopy.getURI());

        BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));

        int size = 0;
        char[] arr = new char[1024];
        while((size = br.read(arr)) != -1){
            bw.write(arr, 0, size);
        }
        br.close();
        bw.flush();
        bw.close();
    }
}

ClassPathResource로 사용하면 build/resources/main/ 경로에 생성된다.

 

 

 

반응형

+ Recent posts