๐Ÿฏ[TIL] 250624-017

byoยท2025๋…„ 6์›” 24์ผ

๐Ÿ’ซ JAVA

โœ”๏ธ Reflection

โœ… ๋ฆฌํ”Œ๋ ‰์…˜๊ณผ ๊ฐ์ฒด ์ง€ํ–ฅ

  • ๋ฆฌํ”Œ๋ ‰์…˜์€ ๊ฐ์ฒด ์ง€ํ–ฅ ์›์น™ ์ค‘ ์บก์Аํ™”(encapsulation) ๋ฅผ ์œ„๋ฐ˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • private ํ•„๋“œ์™€ ๋ฉ”์„œ๋“œ์— ์ ‘๊ทผํ•˜๊ณ  ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
  • ๋˜ํ•œ, ์ •์  ํƒ€์ž… ์•ˆ์ •์„ฑ์„ ํ•ด์น˜๊ณ  ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜ ๊ฐ€๋Šฅ์„ฑ์„ ๋†’์ž…๋‹ˆ๋‹ค.

โœ๏ธ ์˜ˆ์‹œ:

  • Class.forName("ํด๋ž˜์Šค๋ช…") โ†’ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ํด๋ž˜์Šค๋Š” ์ปดํŒŒ์ผ ํƒ€์ž„์— ํ™•์ธ ๋ถˆ๊ฐ€
  • method.invoke() โ†’ ์ž˜๋ชป๋œ ์ธ์ž ์ „๋‹ฌ ์‹œ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์˜ค๋ฅ˜๋ฅผ ์žก์ง€ ๋ชปํ•จ

๐Ÿ” ๋ฆฌํ”Œ๋ ‰์…˜์˜ ๋‚จ์šฉ์€ ์˜์กด์„ฑ ์ฆ๊ฐ€์™€ ์ฝ”๋“œ ๋‚œ๋…ํ™”๋ฅผ ์ดˆ๋ž˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์ž๋™์™„์„ฑ, ๋ฆฌํŒฉํ† ๋ง ๋„๊ตฌ์˜ ๋„์›€์„ ๋ฐ›๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค.
  • ์œ ์ง€๋ณด์ˆ˜์„ฑ์ด ๋–จ์–ด์ง€๊ณ  ํ…Œ์ŠคํŠธ๋„ ์–ด๋ ค์›Œ์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿค” ๊ทธ๋Ÿผ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ๋ฆฌํ”Œ๋ ‰์…˜์ด ํ•„์š”ํ•œ ์ด์œ 

  • ๋ฆฌํ”Œ๋ ‰์…˜์€ ํ”„๋ ˆ์ž„์›Œํฌ์™€ AOP์˜ ํ•ต์‹ฌ ๋„๊ตฌ๋กœ ํ™œ์šฉ๋ฉ๋‹ˆ๋‹ค.

โœ… AOP (Aspect-Oriented Programming)

  • ์Šคํ”„๋ง AOP ๋“ฑ์—์„œ๋Š” ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์ „ํ›„์— ๋ถ€๊ฐ€ ๋กœ์ง์„ ์‚ฝ์ž…ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์ด๋•Œ ๋ฉ”์„œ๋“œ๋ช…, ํŒŒ๋ผ๋ฏธํ„ฐ, ์–ด๋…ธํ…Œ์ด์…˜ ๋“ฑ์€ ๋ฆฌํ”Œ๋ ‰์…˜์œผ๋กœ ์ถ”์ถœ๋ฉ๋‹ˆ๋‹ค.

โœ… DI (Dependency Injection), ORM ๋“ฑ์—์„œ์˜ ์–ด๋…ธํ…Œ์ด์…˜ ๋ถ„์„

  • @Autowired, @Entity ๋“ฑ์˜ ์–ด๋…ธํ…Œ์ด์…˜์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํด๋ž˜์Šค ๊ตฌ์กฐ๋ฅผ ๋ถ„์„ํ•˜๊ณ 
    ๊ฐ์ฒด๋ฅผ ๋™์ ์œผ๋กœ ์ฃผ์ž…ํ•˜๊ฑฐ๋‚˜ ๋งคํ•‘ํ•ฉ๋‹ˆ๋‹ค.

โœ… ํ”Œ๋Ÿฌ๊ทธ์ธ ๊ตฌ์กฐ, ๋Ÿฐํƒ€์ž„ ๊ฐ์ฒด ์กฐ๋ฆฝ ๋“ฑ ์œ ์—ฐํ•œ ์„ค๊ณ„์—๋„ ์œ ์šฉ

  • ์ •ํ•ด์ง€์ง€ ์•Š์€ ํด๋ž˜์Šค๋ฅผ ๋™์ ์œผ๋กœ ๋กœ๋”ฉํ•˜๊ฑฐ๋‚˜ ์‹คํ–‰ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ํ•„์ˆ˜์ ์ž…๋‹ˆ๋‹ค.

๐Ÿ’ก ๊ฒฐ๋ก 

  • ์ง์ ‘ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ํ”„๋ ˆ์ž„์›Œํฌ์— ์˜ํ•ด ๊ฐ„์ ‘์ ์œผ๋กœ ํ™œ์šฉ๋˜๋„๋ก ์„ค๊ณ„ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ด์ƒ์ ์ž…๋‹ˆ๋‹ค.

๐Ÿ” ๋ฆฌํ”Œ๋ ‰์…˜ ์ฃผ์š” API ์š”์•ฝ

๋ฉ”์„œ๋“œ์„ค๋ช…
getName()ํด๋ž˜์Šค ์ด๋ฆ„ ๋ฐ˜ํ™˜
getDeclaredFields()๋ชจ๋“  ํ•„๋“œ ๋ฐฐ์—ด ๋ฐ˜ํ™˜
getDeclaredMethods()๋ชจ๋“  ๋ฉ”์„œ๋“œ ๋ฐฐ์—ด ๋ฐ˜ํ™˜
getDeclaredConstructor()๊ธฐ๋ณธ ์ƒ์„ฑ์ž ๋ฐ˜ํ™˜
newInstance()๊ฐ์ฒด ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
setAccessible(true)private ๋ฉค๋ฒ„ ์ ‘๊ทผ ํ—ˆ์šฉ

โœ”๏ธ Custom Annotation

โœ… ์ปค์Šคํ…€ ์–ด๋…ธํ…Œ์ด์…˜์ด๋ž€?

  • ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ์ •์˜ํ•œ ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ, ์ฝ”๋“œ์— ๋ฉ”ํƒ€ ์ •๋ณด๋ฅผ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ฝ”๋“œ์˜ ์˜๋ฏธ๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ์ „๋‹ฌํ•˜๊ณ , ๊ฐ€๋…์„ฑ๊ณผ ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์—ฌ ์œ ์ง€๋ณด์ˆ˜๋ฅผ ๊ฐ„ํŽธํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

โœ… @interface ์ •์˜

  • ์–ด๋…ธํ…Œ์ด์…˜์€ @interface ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
  • ๋‚ด๋ถ€์— ์„ ์–ธํ•˜๋Š” ์š”์†Œ๋Š” ๋ฉ”์„œ๋“œ ํ˜•ํƒœ์ง€๋งŒ, ์‹ค์ œ๋กœ๋Š” ์–ด๋…ธํ…Œ์ด์…˜์˜ ์†์„ฑ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • ๊ธฐ๋ณธ๊ฐ’์€ default ํ‚ค์›Œ๋“œ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@interface CustomInfo {
    String author();
    String date();
    int version() default 1;
}

โœ… @Target ์„ค์ •

  • @Target์€ ์–ด๋…ธํ…Œ์ด์…˜์ด ์ ์šฉ๋  ์œ„์น˜(๋Œ€์ƒ)๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  • ElementType.TYPE์€ ํด๋ž˜์Šค, ์ธํ„ฐํŽ˜์ด์Šค, ์—ด๊ฑฐํ˜• ๋“ฑ์— ์ ์šฉ ๊ฐ€๋Šฅํ•˜๊ณ ,
  • ElementType.METHOD๋Š” ๋ฉ”์„œ๋“œ์— ์ ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
@Target({ElementType.TYPE, ElementType.METHOD})

โœ… @Retention ์„ค์ •

  • @Retention์€ ์–ด๋…ธํ…Œ์ด์…˜์˜ ์œ ์ง€ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฉ”ํƒ€ ์–ด๋…ธํ…Œ์ด์…˜์ž…๋‹ˆ๋‹ค.
  • RUNTIME์„ ์„ค์ •ํ•˜๋ฉด ์‹คํ–‰ ์ค‘์—๋„ ์–ด๋…ธํ…Œ์ด์…˜ ์ •๋ณด๋ฅผ ๋ฆฌํ”Œ๋ ‰์…˜์œผ๋กœ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • SOURCE, CLASS, RUNTIME 3๊ฐ€์ง€ ์ •์ฑ…์ด ์žˆ์œผ๋ฉฐ, ๊ฐ๊ฐ ์œ ์ง€ ์‹œ์ ์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค.
@Retention(RetentionPolicy.RUNTIME)

โ„น๏ธ retension meaning
= hold


โœ… ํด๋ž˜์Šค์— ์ ์šฉ

  • ํด๋ž˜์Šค ์„ ์–ธ๋ถ€ ์œ„์— ์ปค์Šคํ…€ ์–ด๋…ธํ…Œ์ด์…˜ @CustomInfo๋ฅผ ์‚ฌ์šฉํ•ด ๋ฉ”ํƒ€ ์ •๋ณด๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  • author, date, version ์š”์†Œ ๊ฐ’์„ ๋ชจ๋‘ ๋ช…์‹œํ•ฉ๋‹ˆ๋‹ค.
@CustomInfo(author = "John Doe", date = "2025-03-25", version = 2)
class Demo {
    // ํด๋ž˜์Šค ๋‚ด๋ถ€ ๋กœ์ง ์ƒ๋žต
}

โœ… ๋ฉ”์„œ๋“œ์— ์ ์šฉ

  • ๋ฉ”์„œ๋“œ์—๋„ ์–ด๋…ธํ…Œ์ด์…˜์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • version์€ ๊ธฐ๋ณธ๊ฐ’์„ ์‚ฌ์šฉํ•˜๊ณ , author์™€ date๋งŒ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
@CustomInfo(author = "Jane Smith", date = "2025-03-26")
public void display() {
    System.out.println("Display method executed.");
}

โœ๏ธ ์ „์ฒด์ฝ”๋“œ

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@interface CustomInfo {
    String author();
    String date();
    int version() default 1; // ๋ฉ”์„œ๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ ํ•„๋“œ ๊ฐ’์ž„
}
@CustomInfo(author = "j797", date = "2025-06-24", version = 2)
class Demo {
    @CustomInfo(author = "j797", date = "2025-06-24")
    public void display() {
        System.out.println("Display method executed");
    }
}

โœ… ๋ฆฌํ”Œ๋ ‰์…˜์œผ๋กœ ์–ด๋…ธํ…Œ์ด์…˜ ์ฝ๊ธฐ

  • ํด๋ž˜์Šค๋‚˜ ๋ฉ”์„œ๋“œ์— ๋ถ™์€ ์–ด๋…ธํ…Œ์ด์…˜์„ ์‹คํ–‰ ์ค‘์— ์ฝ๊ณ  ์‹ถ์„ ๋•Œ ๋ฆฌํ”Œ๋ ‰์…˜์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • ๋จผ์ € isAnnotationPresent()๋กœ ์–ด๋…ธํ…Œ์ด์…˜ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๊ณ ,
  • getAnnotation()์œผ๋กœ ์‹ค์ œ ์–ด๋…ธํ…Œ์ด์…˜ ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
Class<?> demoClass = demo.getClass();
if (demoClass.isAnnotationPresent(CustomInfo.class)) {
    CustomInfo info = demoClass.getAnnotation(CustomInfo.class);
    System.out.println("์ž‘์„ฑ์ž: " + info.author());
    System.out.println("์ž‘์„ฑ์ผ: " + info.date());
    System.out.println("๋ฒ„์ „: " + info.version());
}

isAnnotationPresent() ์‹คํ–‰ํ•  ๋•Œ

  1. ๊ทธ๋ƒฅ ์‹คํ–‰ // true
  2. ์–ด๋…ธํ…Œ์ด์…˜ ์ œ๊ฑฐ ํ›„ ์‹คํ–‰ // false
  3. Runtime -> Class ์‹คํ–‰ // false

โœ”๏ธ File I/O

โœ… ์ŠคํŠธ๋ฆผ I/O ๊ฐœ์š”

  • ์ž๋ฐ”์—์„œ๋Š” ์ŠคํŠธ๋ฆผ(Stream)์„ ์ด์šฉํ•ด ํŒŒ์ผ์ด๋‚˜ ๋„คํŠธ์›Œํฌ์™€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›์Šต๋‹ˆ๋‹ค.
  • ์ŠคํŠธ๋ฆผ์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉฐ, ๋ฌธ์ž(char)๋‚˜ ๋ฐ”์ดํŠธ(byte)๋ฅผ ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์”ฉ ์ฝ๊ฑฐ๋‚˜ ์”๋‹ˆ๋‹ค.
  • ๊ตฌ์กฐ๊ฐ€ ๋‹จ์ˆœํ•ด ๋ฐฐ์šฐ๊ธฐ ์‰ฝ์ง€๋งŒ, ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์‹œ์—๋Š” Buffered ๋ฐฉ์‹์ด ๋” ์ ํ•ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

โœ… writeFile ๋ฉ”์„œ๋“œ ๊ตฌํ˜„

  • ํŒŒ์ผ์— ๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜๋Š” writeFile ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.
  • FileWriter๋ฅผ ์‚ฌ์šฉํ•ด ํ…์ŠคํŠธ๋ฅผ ์“ฐ๊ณ , try-with-resources ๊ตฌ๋ฌธ์œผ๋กœ ์ž๋™์œผ๋กœ ํŒŒ์ผ์„ ๋‹ซ๋„๋ก ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
  • ํŒŒ์ผ์ด ์—†์œผ๋ฉด ์ƒˆ๋กœ ์ƒ์„ฑ๋˜๊ณ , ์ด๋ฏธ ์กด์žฌํ•˜๋ฉด ๋‚ด์šฉ์„ ๋ฎ์–ด์”๋‹ˆ๋‹ค.
public static void writeFile(String filename, String content) {
    try (FileWriter writer = new FileWriter(filename)) {
        writer.write(content);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

โœ… FileWriter ์‚ฌ์šฉ๋ฒ•

  • FileWriter๋Š” ํ…์ŠคํŠธ ํŒŒ์ผ์— ๋ฌธ์ž์—ด์„ ์“ฐ๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.
  • ์ƒ์„ฑ์ž์— ํŒŒ์ผ๋ช…์„ ์ „๋‹ฌํ•˜๋ฉด ํ•ด๋‹น ํŒŒ์ผ์„ ์—ด๊ฑฐ๋‚˜ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • write() ๋ฉ”์„œ๋“œ๋กœ ํ…์ŠคํŠธ๋ฅผ ๊ธฐ๋กํ•˜๋ฉฐ, try-with-resources ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•ด ํŒŒ์ผ์„ ์ž๋™์œผ๋กœ ๋‹ซ์Šต๋‹ˆ๋‹ค.
  • ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ธฐ์กด ํŒŒ์ผ ๋‚ด์šฉ์€ ๋ฎ์–ด์“ฐ์—ฌ์ง€๋ฉฐ, ์ด์–ด์“ฐ๊ธฐ(append)๋ฅผ ์›ํ•  ๊ฒฝ์šฐ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ true๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.
public static void main(String[] args) {
    writeFile("example.txt", "Hello, Java I/O!\nThis is a test.");
    System.out.println("ํŒŒ์ผ ์ƒ์„ฑ ์™„๋ฃŒ");
}

โ„น๏ธ try-with-resources

finally ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ตฌ๋ฌธ.

๐Ÿค” ๋งŒ์•ฝ try-with-resources ์—†์ด ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค๋ฉด
try-catch ๊ตฌ๋ฌธ ์•ˆ์—์„œ FileWriter๋กœ ์ž‘์„ฑํ•œ ๋’ค์— ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๊ณ , ํ•ด์ œ๋ฅผ ์œ„ํ•ด finally ๊ตฌ๋ฌธ์—์„œ ๋‹ค์‹œ try-catch ๊ตฌ๋ฌธ์„ ์—ด์–ด FileWriter๋ฅผ ํ•ด์ œํ•˜๊ณ  ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

public static void writeFile(String filename, String content) {
    FileWriter writer = null;
    try {
        writer = new FileWriter(filename);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (writer != null) writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

โœ… try-with-resources ๊ตฌ๋ฌธ์œผ๋กœ ๋ฐ”๊พธ๊ธฐ
try ์˜†์— ๊ด„ํ˜ธ๋ฅผ ์—ด์–ด writeFile ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด FileWriter๋ฅผ ์‚ฌ์šฉํ•ด ํ…์ŠคํŠธ๋ฅผ ์“ฐ๊ณ  ๋‚˜์„œ ์ž๋™์œผ๋กœ ํŒŒ์ผ์„ ๋‹ซ๋„๋ก ์ฒ˜๋ฆฌํ•˜์—ฌ finally ์ดํ›„์˜ ๋‚ด์šฉ์„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

public static void writeFile(String filename, String content) {
    try (FileWriter writer = new FileWriter(filename)) {
        writer.write(content);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

โœ… append ์ด์–ด์“ฐ๊ธฐ ๋ชจ๋“œ
FileWriter ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ํŒŒ์ผ์ด๋ฆ„ ๋’ค์— true๋ฅผ ๋„ฃ์œผ๋ฉด append ๋ชจ๋“œ๊ฐ€ ํ™œ์„ฑํ™”๋˜์–ด ์ด์ „์— ์ผ๋˜ ๋‚ด์šฉ์— ๋ฎ์–ด์“ฐ๊ธฐ ํ•˜์ง€ ์•Š๊ณ  ์ถ”๊ฐ€๋กœ ๋‚ด์šฉ์„ ์ด์–ด์“ธ ์ˆ˜ ์žˆ๋‹ค.

try (FileWriter writer = new FileWriter(filename, true)) {
    writer.write(content);
} catch (IOException e) {
    e.printStackTrace();
}

โœ… readFile ๋ฉ”์„œ๋“œ ๊ตฌํ˜„

  • ์ง€์ •๋œ ํŒŒ์ผ์„ ํ•œ ์ค„์”ฉ ์ฝ๊ณ  ์ถœ๋ ฅํ•˜๋Š” readFile() ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
  • BufferedReader๋Š” ๋‚ด๋ถ€ ๋ฒ„ํผ๋ฅผ ํ™œ์šฉํ•ด ์„ฑ๋Šฅ์„ ๋†’์ด๋ฉฐ, FileReader๋ฅผ ๊ฐ์‹ธ์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • try-with-resources ๊ตฌ๋ฌธ์œผ๋กœ ์ž๋™์œผ๋กœ ๋ฆฌ์†Œ์Šค๋ฅผ ๋‹ซ์Šต๋‹ˆ๋‹ค.
public static void readFile(String filename) {
    try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

โœ… FileReader & BufferedReader ์‚ฌ์šฉ๋ฒ•

  • FileReader๋Š” ํŒŒ์ผ์—์„œ ๋ฌธ์ž ๋‹จ์œ„(char)๋กœ ์ฝ๋Š” ์ŠคํŠธ๋ฆผ์ž…๋‹ˆ๋‹ค.
    ๋”ฐ๋ผ์„œ ํŒŒ์ผ ํฌ๊ธฐ๊ฐ€ ํด ๊ฒฝ์šฐ ๋””์Šคํฌ ์ ‘๊ทผ์ด ๋นˆ๋ฒˆํ•˜๊ฒŒ ๋ฐœ์ƒํ•˜์—ฌ
    ์ž…์ถœ๋ ฅ ํšจ์œจ์ด ๋–จ์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • BufferedReader๋Š” ๋‚ด๋ถ€ ๋ฒ„ํผ๋ฅผ ์‚ฌ์šฉํ•ด ํšจ์œจ์ ์œผ๋กœ ์ฝ๊ณ ,
    ํ•œ ์ค„์”ฉ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    ๋ฒ„ํผ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด๋“ค์ด๊ธฐ ๋•Œ๋ฌธ์—, ํ•œ ๋ฒˆ์— ๋งŽ์€ ์–‘์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์™€ ๋””์Šคํฌ ์ ‘๊ทผ ํšŸ์ˆ˜๋ฅผ ์ค„์ž…๋‹ˆ๋‹ค.
    ๋”ฐ๋ผ์„œ ํŒŒ์ผ ํฌ๊ธฐ๊ฐ€ ํฌ๋”๋ผ๋„ ์ƒ๋Œ€์ ์œผ๋กœ ํšจ์œจ์ ์ธ ๋ฐ์ดํ„ฐ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
  • readLine()์ด null์„ ๋ฐ˜ํ™˜ํ•  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ํŒŒ์ผ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

โœ… ํŒŒ์ผ ์ฝ๊ธฐ ์‹คํ–‰

  • main ๋ฉ”์„œ๋“œ์—์„œ readFile("example.txt")๋ฅผ ํ˜ธ์ถœํ•ด ํŒŒ์ผ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
  • ํŒŒ์ผ์— ์ €์žฅ๋œ ๋ฌธ์ž์—ด์ด ํ•œ ์ค„์”ฉ ์ฝ˜์†”์— ์ถœ๋ ฅ๋˜๋ฉฐ, ํŒŒ์ผ์ด ์ •์ƒ์ ์œผ๋กœ ์ฝํ˜”๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
public static void main(String[] args) {
    readFile("example.txt");
}

โœ๏ธ ํ†ตํ•ฉ ์ฝ”๋“œ

import java.io.*;

public class Main {
    public static void writeFile(String filename, String content) {
        try (FileWriter writer = new FileWriter(filename, true)) {
            writer.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void readFile(String filename) {
        try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
            String Line;
            while ((Line = reader.readLine()) != null) {
                System.out.println(Line);
            }
        } catch (IOException e) {
             e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        writeFile("lunch-menu.txt", "lunch-menu");
        writeFile("lunch-menu.txt", "๋–ก๋ณถ์ด");
        System.out.println("ํŒŒ์ผ ์ƒ์„ฑ ์™„๋ฃŒ");
        readFile("lunch-menu.txt");
    }
}

โ–ถ๏ธ
ํŒŒ์ผ ์ƒ์„ฑ ์™„๋ฃŒ
lunch-menu๋–ก๋ณถ์ด

โœ”๏ธ NIO

โœ… NIO ๊ฐœ์š”

  • NIO(New I/O)๋Š” ๋ฒ„ํผ(Buffer)์™€ ์ฑ„๋„(Channel)์„ ์‚ฌ์šฉํ•ด ๊ณ ์„ฑ๋Šฅ ํŒŒ์ผ ์ž…์ถœ๋ ฅ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.
  • ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ ์ „์†ก ๋ฐ ๋ฉ€ํ‹ฐํ”Œ๋ ‰์‹ฑ(๋™์‹œ ํŒŒ์ผ/๋„คํŠธ์›Œํฌ I/O)์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค.

โœ… ์ดˆ๊ธฐํ™” ๋ฐ import

  • main() ๋ฉ”์„œ๋“œ ๋‚ด์šฉ์„ ๋น„์šฐ๊ณ , NIO ๊ด€๋ จ ํด๋ž˜์Šค๋ฅผ import ํ•ฉ๋‹ˆ๋‹ค.
  • java.nio.file.*, java.nio.channels.FileChannel, java.nio.ByteBuffer, java.io.IOException ํŒจํ‚ค์ง€๋ฅผ ์ค€๋น„ํ•˜์„ธ์š”.
import java.nio.file.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        // NIO ์‹ค์Šต ์ค€๋น„
    }
}

โœ… writeFile ๋ฉ”์„œ๋“œ ์ƒ์„ฑ

  • ํŒŒ์ผ์— ๋ฌธ์ž์—ด์„ ์“ฐ๋Š” writeFile ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•˜๊ณ , Path๋ฅผ ์‚ฌ์šฉํ•ด ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
public static void writeFile() {
    Path filePath = Paths.get("example.txt");
    String content = "Hello, NIO World!";
    // ์ฑ„๋„๊ณผ ๋ฒ„ํผ๋กœ ์“ฐ๊ธฐ ๋กœ์ง ์ถ”๊ฐ€
}

โœ… Channel ์—ด๊ธฐ & Buffer ์ค€๋น„

  • FileChannel.open()์— CREATE, WRITE ์˜ต์…˜์œผ๋กœ ์ฑ„๋„์„ ์—ด๊ณ ,
  • ByteBuffer.allocate()๋กœ ๋ฒ„ํผ๋ฅผ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค.
try (FileChannel writeChannel = FileChannel.open(
         filePath,
         StandardOpenOption.CREATE,
         StandardOpenOption.WRITE)) {
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    // ๋ฒ„ํผ์— ๋ฐ์ดํ„ฐ ์“ฐ๊ธฐ
}

โœ… ๋ฒ„ํผ์— ๋ฐ์ดํ„ฐ ์“ฐ๊ธฐ

  • put()์œผ๋กœ ๋ฒ„ํผ์— ๋ฌธ์ž์—ด์˜ ๋ฐ”์ดํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์Šต๋‹ˆ๋‹ค.
  • flip()์„ ํ˜ธ์ถœํ•ด ์“ฐ๊ธฐ ๋ชจ๋“œ๋ฅผ ์ฝ๊ธฐ ๋ชจ๋“œ๋กœ ์ „ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ์ดํ›„ write()๋กœ ๋ฒ„ํผ์˜ ๋‚ด์šฉ์„ ์ฑ„๋„์— ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.
buffer.put(content.getBytes());
buffer.flip();
writeChannel.write(buffer);
System.out.println("File written successfully.");

โœ… readFile ๋ฉ”์„œ๋“œ ์ƒ์„ฑ

  • readFile ๋ฉ”์„œ๋“œ๋Š” ํŒŒ์ผ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ธฐ ์œ„ํ•ด ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
  • Paths.get()์œผ๋กœ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์„ค์ •ํ•˜๊ณ , FileChannel.open()์— READ ์˜ต์…˜์„ ์‚ฌ์šฉํ•ด ์ฑ„๋„์„ ์—ฝ๋‹ˆ๋‹ค.
  • ByteBuffer.allocate()๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์„ ๋ฒ„ํผ๋ฅผ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค.
public static void readFile() {
    Path filePath = Paths.get("example.txt");
    try (FileChannel readChannel = FileChannel.open(
             filePath,
             StandardOpenOption.READ)) {
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        // ์ฝ๊ธฐ ๋กœ์ง ์ถ”๊ฐ€ ์˜ˆ์ •
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}

โœ… ๋ฒ„ํผ๋ฅผ ์ด์šฉํ•œ ํŒŒ์ผ ์ฝ๊ธฐ

  • read()๋กœ ํŒŒ์ผ ๋‚ด์šฉ์„ ByteBuffer์— ์ฝ์Šต๋‹ˆ๋‹ค.
  • flip()์œผ๋กœ ์ฝ๊ธฐ ๋ชจ๋“œ๋กœ ์ „ํ™˜ํ•œ ๋’ค, hasRemaining()๊ณผ get()์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌธ์ž๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
  • clear()๋กœ ๋ฒ„ํผ๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ ํ›„ ๋‹ค์‹œ read()๋ฅผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.
int bytesRead = readChannel.read(buffer);
while (bytesRead != -1) {
    buffer.flip();
    while (buffer.hasRemaining()) {
        System.out.print((char) buffer.get());
    }
    buffer.clear();
    bytesRead = readChannel.read(buffer);
}
System.out.println("\nFile read successfully.");

โœ๏ธ ํ†ตํ•ฉ ์ฝ”๋“œ

public class Main {
    public static void writeFile(String filename, String content) {
        Path filePath = Paths.get(filename);
        System.out.println(filePath);

//        try (FileWriter fileWriter = new FileWriter(filePath.toFile())) {
//            fileWriter.write(content);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        try (FileChannel writeChannel = FileChannel.open(filePath, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
            ByteBuffer buffer = ByteBuffer.allocate(1024); //1024byte = 1kb
            // BUFFER READ MODE
            buffer.put(content.getBytes()); // ํ…์ŠคํŠธ๋ฅผ .getBytes()๋ฅผ ํ†ตํ•ด buffer์— byte ๋‹จ์œ„๋กœ ๋„ฃ๋Š”๋‹ค.
            System.out.println("Byte content: " + Arrays.toString(content.getBytes()));

            // FILE WRITE MODE
            buffer.flip(); // CREATE -> WRITE mode
            writeChannel.write(buffer); // buffer ์˜ ๋‚ด์šฉ์„ file์— write
            System.out.println("ํŒŒ์ผ์„ ์„ฑ๊ณต์ ์œผ๋กœ ์ž‘์„ฑ ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void readFile(String filename) {
        Path filePath = Paths.get(filename);
        try (FileChannel readChannel = FileChannel.open(filePath, StandardOpenOption.READ)) {
            ByteBuffer buffer = ByteBuffer.allocate(1024);

            int bytesRead = readChannel.read(buffer);
            while (bytesRead != -1) {
                buffer.flip();

                String chunk = StandardCharsets.UTF_8.decode(buffer).toString();
                System.out.print(chunk);


                buffer.clear();
                bytesRead = readChannel.read(buffer);

//                while (buffer.hasRemaining()) {
//                    System.out.println((char) buffer.get());
//                }
//                buffer.clear();
//                bytesRead = readChannel.read(buffer);
//                System.out.println(bytesRead);
            }
            System.out.println("\nํŒŒ์ผ ์ฝ๊ธฐ๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
//        writeFile("lunch-menu.txt", "์˜ค๋Š˜์˜ ์ ์‹ฌ ๋ฉ”๋‰ด๋Š” ๋ญ˜๊นŒ์š”?");
        readFile("lunch-menu.txt");
    }
}

โ†”๏ธ ์ŠคํŠธ๋ฆผ I/O vs NIO ๋น„๊ต

๊ตฌ๋ถ„์ŠคํŠธ๋ฆผ I/ONIO (New I/O)
์ฒ˜๋ฆฌ ๋‹จ์œ„๋ฐ”์ดํŠธ ๋˜๋Š” ๋ฌธ์ž ๋‹จ์œ„๋กœ ์ˆœ์ฐจ ์ฒ˜๋ฆฌ๋ฒ„ํผ(Buffer)๋ฅผ ์‚ฌ์šฉํ•œ ๋ธ”๋ก ๋‹จ์œ„ ์ฒ˜๋ฆฌ
์„ฑ๋Šฅ๊ฐ„๋‹จํ•œ ์ž…์ถœ๋ ฅ์— ์ ํ•ฉ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๋ฐ ๋ฉ€ํ‹ฐ I/O ์ƒํ™ฉ์— ์œ ๋ฆฌ
์ฃผ์š” APIFileInputStream, FileOutputStream, FileReader, FileWriterPath, FileChannel, ByteBuffer ๋“ฑ
profile
๐Ÿ—‚๏ธ hamstern

0๊ฐœ์˜ ๋Œ“๊ธ€