All :L

[Java] ํŒŒ์ผ ์ž…์ถœ๋ ฅ (File/IO) ๋ณธ๋ฌธ

STUDY/Java

[Java] ํŒŒ์ผ ์ž…์ถœ๋ ฅ (File/IO)

ofijwe 2024. 11. 3. 12:25
๋ฐ˜์‘ํ˜•

๐Ÿ“’ ๋…ธ๋“œ์ŠคํŠธ๋ฆผ

1๏ธโƒฃ I/O์™€ Stream

  • I/O → Input/Output
  • ๋ฐ์ดํ„ฐ๋Š” ํ•œ์ชฝ์—์„œ ์ฃผ๊ณ  ํ•œ์ชฝ์—์„œ ๋ฐ›๋Š” ๊ตฌ์กฐ๋กœ ๋˜์–ด์žˆ์Œ
    • ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์˜ ๋๋‹จ → ๋…ธ๋“œ(node)
    • ๋‘ ๋…ธ๋“œ๋ฅผ ์—ฐ๊ฒฐํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐœ๋… → ์ŠคํŠธ๋ฆผ(stream)
    • ์ŠคํŠธ๋ฆผ์€ ๋‹จ๋ฐฉํ–ฅ ํ†ต์‹ ๋งŒ ๊ฐ€๋Šฅ
    • ํ•˜๋‚˜์˜ ์ŠคํŠธ๋ฆผ์œผ๋กœ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์„ ๊ฐ™์ด ์ฒ˜๋ฆฌํ•  ์ˆ˜ X
  • Node Stream์˜ ์ข…๋ฅ˜์™€ naming

2๏ธโƒฃ InputStream๊ณผ Reader

  • InputStream์˜ ์ฃผ์š” ๋ฉ”์„œ๋“œ
    • read() → ๋ฐ์ดํ„ฐ ์ฝ์–ด์„œ ๋ฐ˜ํ™˜
      • public abstract int read() throws IOException → byte ํ•œ ๊ฐœ์”ฉ ์ฝ์Œ
      • public int read(byte b[]) throws IOException
      • public int read(byte b[], int offset, int len) throws IOException
    • close() → ์ŠคํŠธ๋ฆผ ์ข…๋ฃŒํ•ด์„œ ์ž์› ๋ฐ˜๋‚ฉ
      • public void close() throws IOException
  • Reader์˜ ์ฃผ์š” ๋ฉ”์„œ๋“œ
    • read()
      • public int read() throws IOExction → char ํ•œ ๊ฐœ์”ฉ ์ฝ์Œ
      • public int read(char cbuf[] throws IOException → buffer๋งŒํผ ์”ฉ ์ฝ์Œ
      • abstract public int read(char cbuf[], int off, int len) throws IOException
      • public int read(java.nio.CharBuffer target) throws IOException
    • close()
      • public void close() throws IOException

3๏ธโƒฃ OutputStream๊ณผ Writer

  • OutputStream์˜ ์ฃผ์š” ๋ฉ”์„œ๋“œ
    • write() → ์ž…๋ ฅ๋ฐ›์€ ๋‚ด์šฉ์„ byte, string ๋“ฑ์œผ๋กœ ๋ณ€ํ™˜ํ•ด์„œ ์ถœ๋ ฅ
    • close() → ์ŠคํŠธ๋ฆผ ์ข…๋ฃŒํ•ด์„œ ์ž์› ๋ฐ˜๋‚ฉ
    • flush() → ๋ฒ„ํผ๊ฐ€ ์žˆ๋Š” ์ŠคํŠธ๋ฆผ์—์„œ ๋ฒ„ํผ ๋‚ด์šฉ ์ถœ๋ ฅ ํ›„ ๋ฒ„ํผ clear
  • Writer์˜ ์ฃผ์š” ๋ฉ”์„œ๋“œ
    • write() → ์ž…๋ ฅ๋ฐ›์€ ๋‚ด์šฉ์„ char, string ๋“ฑ์œผ๋กœ ๋ณ€ํ™˜ํ•ด์„œ ์ถœ๋ ฅ
    • append() → ์ถœ๋ ฅ ํ›„ ๋ฆฌํ„ด
    • close() → ๋‚ด๋ถ€์ ์œผ๋กœ flush() ํ˜ธ์ถœ
    • flush() → ๋ฒ„ํผ๊ฐ€ ์žˆ๋Š” ์ŠคํŠธ๋ฆผ์—์„œ ๋ฒ„ํผ ๋‚ด์šฉ ์ถœ๋ ฅ ํ›„ ๋ฒ„ํผ clear

4๏ธโƒฃ ๋…ธ๋“œ ์ŠคํŠธ๋ฆผ ํ™œ์šฉ

  • File
    • ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์ž…์ถœ๋ ฅ ์žฅ์น˜ ์ค‘ ํ•˜๋‚˜๋กœ ํŒŒ์ผ๊ณผ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๋‹ค๋ฃจ๋Š” ํด๋ž˜์Šค
  • File์˜ ๋ฉ”์„œ๋“œ
    • File() → ํŒŒ์ผ ์ƒ์„ฑ
    • createNewFile() → ์ƒˆ๋กœ์šด ๋ฌผ๋ฆฌ์ ์ธ ํŒŒ์ผ ์ƒ์„ฑ
    • mkdir() → ์ƒˆ๋กœ์šด ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ
    • mkdirs() → ๊ฒฝ๋กœ ์ƒ ์—†๋Š” ๋ชจ๋“  ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ
    • delete() → ํŒŒ์ผ ๋˜๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ ์‚ญ์ œ
    • getName(), getPath(), getAbsolutePath(), getCanonicalPath() → ์ด๋ฆ„, ๊ฒฝ๋กœ, ์ ˆ๋Œ€ ๊ฒฝ๋กœ, ์ •์‹ ๊ฒฝ๋กœ ๋ฆฌํ„ด
    • isDirectory(), isFile() → ๋””๋ ‰ํ† ๋ฆฌ์ธ์ง€ ํŒŒ์ผ์ธ์ง€ ๋ฆฌํ„ด
    • length() → ํŒŒ์ผ ๊ธธ์ด ๋ฆฌํ„ด
    • listFiles() → ํŒŒ์ผ์ด ๋””๋ ‰ํ† ๋ฆฌ์ธ ๊ฒฝ์šฐ ์ž์‹ ํŒŒ์ผ๋“ค์„ File[]ํ˜•ํƒœ๋กœ ๋ฆฌํ„ด
  • FileInputStream & FileOutputStream
    • FileInputStream() → name ๊ฒฝ๋กœ์˜ ํŒŒ์ผ์„ ์ฝ๋Š” ์ŠคํŠธ๋ฆผ ์ƒ์„ฑ
    • FileOuputStream() → name ๊ฒฝ๋กœ ํŒŒ์ผ์— ์ถœ๋ ฅํ•˜๋Š” ์ŠคํŠธ๋ฆผ ์ƒ์„ฑ / ๊ธฐ์กด ํŒŒ์ผ์ด ์žˆ๋‹ค๋ฉด ์ด์–ด์„œ ์ž‘์„ฑ

๐Ÿ“’ ๋ณด์กฐ์ŠคํŠธ๋ฆผ

1๏ธโƒฃ ๋ณด์กฐ ์ŠคํŠธ๋ฆผ ๊ฐœ๋…๊ณผ ์ข…๋ฅ˜

  • ๋ณด์กฐ ์ŠคํŠธ๋ฆผ(Filter Stream, Processing Stream)
    • ๋‹ค๋ฅธ ์ŠคํŠธ๋ฆผ์— ๋ถ€๊ฐ€์ ์ธ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ์ŠคํŠธ๋ฆผ
    • ์ŠคํŠธ๋ฆผ ์ฒด์ด๋‹(Stream Chaining) → ํ•„์š”์— ๋”ฐ๋ผ ์—ฌ๋Ÿฌ ๋ณด์กฐ ์ŠคํŠธ๋ฆผ ์—ฐ๊ฒฐํ•ด์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • ๋ณด์กฐ ์ŠคํŠธ๋ฆผ ์ข…๋ฅ˜
    • byte ์ŠคํŠธ๋ฆผ์„ char ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋ณ€ํ™˜
      • InputStreamReader, OutputStreamWriter → byte ๊ธฐ๋ฐ˜
    • ๋ฒ„ํผ๋ง์„ ํ†ตํ•œ ์†๋„ ํ–ฅ์ƒ
      • BufferedInputStream, BufferedOutputStream → byte ๊ธฐ๋ฐ˜
      • BufferedReader, BufferedWriter → char ๊ธฐ๋ฐ˜
    • ๊ฐ์ฒด ์ „์†ก
      • ObjectInputStream, ObjectOutputStrema → byte ๊ธฐ๋ฐ˜
    • ์ƒ์„ฑ → ์ด์ „ ์ŠคํŠธ๋ฆผ์„ ์ƒ์„ฑ์ž์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์— ์—ฐ๊ฒฐ
    • ์ข…๋ฃŒ → ๋ณด์กฐ ์ŠคํŠธ๋ฆผ์˜ close()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋…ธ๋“œ ์ŠคํŠธ๋ฆผ์˜ close()๊นŒ์ง€ ํ˜ธ์ถœ ๋จ.
  • ์‚ฌ์šฉ ์ŠคํŠธ๋ฆผ ๊ฒฐ์ •๊ณผ์ •
    • ๋…ธ๋“œ๊ฐ€ ๋ญ”์ง€ → ํƒ€์ž…์€ ๋ญ”์ง€(byte? String?) → ๋ฐฉํ–ฅ์ด ๋ญ”์ง€ → ์ถ”๊ฐ€ ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•œ์ง€

2๏ธโƒฃ ๋ณด์กฐ ์ŠคํŠธ๋ฆผ ํ™œ์šฉ

  • InputStreamReader & OutputStreamWriter
    • byte ๊ธฐ๋ฐ˜ ์ŠคํŠธ๋ฆผ์„ char ๊ธฐ๋ฐ˜์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ๋Š” ์ŠคํŠธ๋ฆผ
      • ๋ฌธ์ž์—ด ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด byte ๋‹จ์œ„๋ณด๋‹ค char ๋‹จ์œ„๊ฐ€ ์œ ๋ฆฌ
      • ํ‚ค๋ณด๋“œ์—์„œ ์ž…๋ ฅ๋ฐ›์€ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉ
    • ๋ณ€ํ™˜ ์‹œ encoding ์ง€์ • ๊ฐ€๋Šฅ
      • InputStramReader(), OutputStreamWriter()
  • Buffered ๊ณ„์—ด
    • ์ŠคํŠธ๋ฆผ์˜ ์ž…/์ถœ๋ ฅ ํšจ์œจ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด ๋ฒ„ํผ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ŠคํŠธ๋ฆผ
      • BufferedInputStream()
      • BufferedOutputStream()
    • BufferedReader & BufferedWriter
      • BufferedReader() → readLine()์€ ์ค„ ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์Œ
      • BufferedWriter()

3๏ธโƒฃ ๊ฐ์ฒด ์ง๋ ฌํ™”(serialization)

  • ๊ฐ์ฒด๋ฅผ ํŒŒ์ผ ๋“ฑ์— ์ €์žฅํ•˜๊ฑฐ๋‚˜ ๋„คํŠธ์›Œํฌ๋กœ ์ „์†กํ•˜๊ธฐ ์œ„ํ•ด ์—ฐ์†์ ์ธ ๋ฐ์ดํ„ฐ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ
  • ๋ฐ˜๋Œ€์˜ ๊ฒฝ์šฐ๋Š” ์—ญ ์ง๋ ฌํ™”(deserialization)
  • ์ง๋ ฌํ™” ๋˜๊ธฐ ์œ„ํ•œ ์กฐ๊ฑด
    • Serializable ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„ํ•  ๊ฒƒ
    • ํด๋ž˜์Šค์˜ ๋ชจ๋“  ๋ฉค๋ฒ„๊ฐ€ Serializable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•จ
    • ์ง๋ ฌํ™”์—์„œ ์ œ์™ธํ•˜๋ ค๋Š” ๋ฉค๋ฒ„๋Š” transient ์„ ์–ธ
  • SerialVersionUID
    • ํด๋ž˜์Šค์˜ ๋ณ€๊ฒฝ ์—ฌ๋ถ€๋ฅผ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•œ ์œ ์ผ ํ‚ค
    • ์ง๋ ฌํ™”ํ•  ๋•Œ UID์™€ ์—ญ ์ง๋ ฌํ™”ํ•  ๋•Œ์˜ UID๊ฐ€ ๋‹ค๋ฅผ ๊ฒฝ์šฐ ์˜ˆ์™ธ ๋ฐœ์ƒ
    • ์ง๋ ฌํ™”๋˜๋Š” ๊ฐ์ฒด์— UID๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ž๋™ ์ƒ์„ฑ
    • ์ง๋ ฌํ™”๋˜๋Š” ๊ฐ์ฒด์— ๋Œ€ํ•ด serialVersionUID ์„ค์ • ๊ถŒ์žฅ

4๏ธโƒฃ ObjectInputStream & ObjectOutputStream

5๏ธโƒฃ Scanner & BufferedReader

  • char ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ธฐ์œ„ํ•œ ํด๋ž˜์Šค๋“ค
  • Scanner → ์ž๋™ ํ˜•๋ณ€ํ™˜์„ ์ง€์›ํ•˜๋Š” ๋“ฑ ์‚ฌ์šฉ์ด ๊ฐ„ํŽธํ•˜์ง€๋งŒ ์†๋„๊ฐ€ ๋Š๋ฆผ
  • BufferedReader → ์ง์ ‘ ์ŠคํŠธ๋ฆผ์„ ๊ตฌ์„ฑํ•ด์•ผ ํ•˜๋Š” ๋“ฑ ๋ฒˆ๊ฑฐ๋กญ์ง€๋งŒ ์†๋„๊ฐ€ ๋น ๋ฆ„
๋ฐ˜์‘ํ˜•

'STUDY > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Java] ์˜ˆ์™ธ (Exception)  (0) 2024.11.01
[Java] ์ปฌ๋ ‰์…˜ (Collection)  (1) 2024.10.30
[Java] ์ œ๋„ค๋ฆญ (Generic)  (2) 2024.10.28
[Java] ์ธํ„ฐํŽ˜์ด์Šค (Interface)  (0) 2024.10.26
[Java] ๋‹คํ˜•์„ฑ (Polymorphism)  (0) 2024.10.24
Comments