Programming/Java

F016 - File, I/O, Stream

osean 2021. 6. 14. 01:26
โœ๏ธ ์‹ค๋ฌด์—์„œ ์—‘์…€์ด๋‚˜ CSV ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ฑฐ๋‚˜ ์—…๋กœ๋“œํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ–ˆ๋˜ ๊ฒฝํ—˜์ด ์žˆ๋Š”๋ฐ, ์—‘์…€์˜ ๊ฒฝ์šฐ Apache ์žฌ๋‹จ์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•ด์„œ ๊ตฌํ˜„ํ–ˆ๊ณ , CSV์˜ ๊ฒฝ์šฐ ByteArrayInputStream ์œผ๋กœ ๊ตฌํ˜„ํ–ˆ์—ˆ๋‹ค.
ํ•˜์ง€๋งŒ ๋‹น์‹œ์—๋Š” ๊ทธ๋ƒฅ ์ด ํด๋ž˜์Šค๋ฅผ ์“ฐ๋ฉด ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œ/์—…๋กœ๋“œ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ๋งŒ ์•Œ๊ณ  ์–ด๋–ป๊ฒŒ ๋‹ค์šด๋กœ๋“œ/์—…๋กœ๋“œ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ์ง€ ๋ชปํ–ˆ๋‹ค. ์ด๋ฒˆ ์‹œ๊ฐ„์„ ํ†ตํ•ด I/O, File, I/O Stream ์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•ด๋ณด์ž!

File

java.io ํŒจํ‚ค์ง€์— ์†ํ•˜๋Š” ํด๋ž˜์Šค๋กœ ํŒŒ์ผ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๊นŒ์ง€ ํฌํ•จํ•˜๋Š” ํฌ๊ด„์ ์ธ ํด๋ž˜์Šค์ด๋‹ค. ๋•Œ๋ฌธ์— ๋ชฉ์ ์˜ ๋ชจํ˜ธ์„ฑ ๋•Œ๋ฌธ์— Java 7 ๋ถ€ํ„ฐ NIO2 ํŒจํ‚ค์ง€๋ฅผ ์ œ๊ณตํ•˜๋ฉด์„œ Files ํด๋ž˜์Šค๊ฐ€ ์ถ”๊ฐ€๋˜๋Š”๋ฐ, ์ด๋Š” File ํด๋ž˜์Šค ๋Œ€๋ถ€๋ถ„์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ๋Œ€์ฒดํ•˜์—ฌ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  File ํด๋ž˜์Šค๋Š” ์ง์ ‘ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์•ผ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์— ๋ฐ˜ํ•ด, Files ํด๋ž˜์Šค๋Š” ๋ชจ๋“  ๋ฉ”์†Œ๋“œ๋ฅผ static ์œผ๋กœ ์„ ์–ธ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ณ„๋„๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์ค„์–ด ๋“ค์—ˆ๋‹ค.

I/O

Input๊ณผ Output์„ ์˜๋ฏธํ•˜๋ฉฐ, Input์€ ๋งˆ์šฐ์Šค์˜ ํด๋ฆญ์ด๋‚˜ ํ‚ค๋ณด๋“œ์˜ ์ž…๋ ฅ ๋“ฑ์ด ์†ํ•˜๊ณ , Output์€ ์Šคํ”ผ์ปค๋‚˜ ๋ชจ๋‹ˆํ„ฐ ํ™”๋ฉด์˜ ์ถœ๋ ฅ ๋“ฑ์ด ์†ํ•œ๋‹ค. ์ฆ‰, java.io ํŒจํ‚ค์ง€์— ์†ํ•œ ํด๋ž˜์Šค๋“ค์€ Stream์„ ํ†ตํ•ด ๋‹จ๋ฐฉํ–ฅ์œผ๋กœ JVM ์™ธ๋ถ€์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ฑฐ๋‚˜ ์“ธ ์ˆ˜ ์žˆ๋‹ค.

ํ•ด๋‹น ๋ถ€๋ถ„์„ ๊ณต๋ถ€ํ•˜๋ฉด์„œ Java๊ฐ€ ํŒŒ์ผ์„ ์ฝ๊ณ  ์“ฐ๋Š” ๋ฐฉ๋ฒ•์ด ๊ถ๊ธˆํ•ด์กŒ๋‹ค.
Java๋Š” FileSystem ์ด๋ผ๋Š” ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด OS์˜ ํŒŒ์ผ ์‹œ์Šคํ…œ์— ์ ‘๊ทผ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋ฉค๋ฒ„ ํ•„๋“œ์˜ ๋ช‡ ๋ถ€๋ถ„์— @Native ์–ด๋…ธํ…Œ์ด์…˜์ด ์„ ์–ธ๋˜์–ด JNI๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๋ฅผ ํ†ตํ•ด Java๋Š” JVM๊ณผ JNI๋ฅผ ํ†ตํ•ด ํŒŒ์ผ ์ ‘๊ทผ ๋ฐฉ์‹์„ ๋žฉํ•‘ํ•ด์„œ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

์ฆ‰, ํŒŒ์ผ์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” OS์˜ ์ปค๋„์„ ํ†ตํ•ด ์ ‘๊ทผํ•ด์•ผ ํ•˜๋ฉฐ, ์ปค๋„์— ํŒŒ์ผ ์ ‘๊ทผ ๋ช…๋ น์„ ๋‚ด๋ฆฌ๊ธฐ ์œ„ํ•ด์„œ๋Š” System Call์„ ์ด์šฉํ•ด ๋ช…๋ น์„ ๋‚ด๋ ค์•ผ ํ•˜๋Š”๋ฐ, ์ด๋ฅผ JVM์„ ํ†ตํ•ด ์ถ”์ƒํ™”ํ•˜์—ฌ ์ ‘๊ทผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

1. JVM์ด OS์˜ ํŒŒ์ผ ์‹œ์Šคํ…œ์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•?

 

How JVM access the file system

I have an argument with my boss who is convinced that the JVM uses JNI to access native things like the file system. I think he is wrong because the JVM itself is a native code and it communicates

stackoverflow.com

2. JVM์€ ์–ด๋–ป๊ฒŒ ํŒŒ์ผ ์ฝ๊ธฐ/์“ฐ๊ธฐ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š”๊ฐ€?

 

How do the JVM and Java implement reading from and writing to files?

In Java, you can read from and write to files. But the JVM can run on many systems that may have different ways of storing files and related data. How does one write JVM code that works on every sy...

stackoverflow.com

3. JVM์ด ํŒŒ์ผ์„ ์ฝ์„ ๋•Œ System Call ์ง์ ‘ ์ˆ˜ํ–‰ํ•˜๋Š”๊ฐ€?

 

Does a file read from a Java application invoke a system call?

My understanding is that a user application requesting a file system path (eg. /aFile) will invoke the File System and get back the virtual address of the requested file. Then the application will

stackoverflow.com

JDK 1.4 ์ด์ „์—๋Š” Stream์ด๋ผ๋Š” ๋‹จ๋ฐฉํ–ฅ ํ†ต๋กœ๋ฅผ ํ†ตํ•ด ๋ฆฌ์†Œ์Šค๋ฅผ ์“ฐ๊ณ  ์ฝ์—ˆ๋Š”๋ฐ, Stream์€ ๋ฆฌ์†Œ์Šค๋ฅผ ์ฝ๊ณ  ์“ฐ๊ธฐ ์œ„ํ•ด์„œ ์ด 2๊ฐœ์˜ Stream์„ ์ƒ์„ฑํ•ด์•ผ ํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์žˆ๊ณ , ํ•ด๋‹น ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋™์•ˆ ๋‹ค๋ฅธ ์ž‘์—…์€ ์ˆ˜ํ–‰ ํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๋‹จ์ (Blocking, Sync)์ด ์กด์žฌํ•œ๋‹ค.

์ด๋ฅผ ๊ฐœ์„ ํ•œ NIO ๋ผ๋Š” ํŒจํ‚ค์ง€๋ฅผ JDK 1.4์— ์ง€์›ํ•˜๋ฏ€๋กœ์จ ๋ฆฌ์†Œ์Šค ์ ‘๊ทผ ๋ฐฉ์‹์ด Stream ๋ง๊ณ ๋„ Buffer์™€ Channel ์ด ์ถ”๊ฐ€๋˜๋ฉด์„œ ๋น„๋™๊ธฐ์‹, Non-Blocking ๋ฐฉ์‹์œผ๋กœ ๋” ๋น ๋ฅด๊ณ  ์œ ์—ฐํ•˜๊ฒŒ ์ ‘๊ทผ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.

ํ•ด๋‹น ๋ถ€๋ถ„์€ Buffer์™€ Channel, Sync/Async, Blocking/Non-Blocking์„ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ์ž์„ธํžˆ ์•Œ์•„๋ณด์ž.

Stream

Stream์ด๋ž€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๋‹จ๋ฐฉํ–ฅ ํ†ต๋กœ๋กœ ํ‚ค๋ณด๋“œ๋‚˜ ๋งˆ์šฐ์Šค ๋“ฑ์˜ ์žฅ์น˜๋กœ ์ž…๋ ฅํ•˜์—ฌ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์„ InputStream, ์™ธ๋ถ€์˜ ์„œ๋ฒ„๋กœ์˜ ๋ฐ์ดํ„ฐ ์ „์†ก์ด๋‚˜ ๋ชจ๋‹ˆํ„ฐ, ์Šคํ”ผ์ปค ๋“ฑ์˜ ์žฅ์น˜๋กœ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ OutputStream ์ด๋ผ๊ณ  ํ•œ๋‹ค.

InputStream๊ณผ OutputStream ํด๋ž˜์Šค ๋‚ด๋ถ€์—๋Š” Closeable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ์ด๋Š” Stream์ด ๋ฆฌ์†Œ์Šค์— ์ ‘๊ทผ ํ•  ๋•Œ Blocking, Synchronous(๋™๊ธฐ) ๋ฐฉ์‹์œผ๋กœ ์ž‘๋™ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•œ๋ฒˆ Stream์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•˜๊ณ  ํ•ด๋‹น Stream์˜ close() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ Stream์„ ๋‹ซ์ง€ ์•Š์œผ๋ฉด ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ข…๋ฃŒ๋  ๋•Œ๊นŒ์ง€ Stream์ด ์—ด๋ ค ์žˆ๋Š” ์ƒํƒœ๊ฐ€ ๋˜๋ฉฐ, ์ด ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ์ž‘์—…์€ ๋ชปํ•˜๊ธฐ์— ์—„์ฒญ๋‚œ ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๊ฐ€ ๋ฐœ์ƒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋˜ํ•œ, Stream์€ ๋‹จ๋ฐฉํ–ฅ ํ†ต๋กœ๋ผ๋Š” ๋‹จ์ ์ด ์žˆ๋Š”๋ฐ, ์ด๋Š” ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•ด ์ฝ๊ธฐ๋กœ ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” InputStream์œผ๋กœ Stream์„ ์ƒ์„ฑํ•˜์—ฌ ์ ‘๊ทผํ•ด์•ผ ํ•˜๊ณ , ์“ฐ๊ธฐ๋กœ ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” OutputStream์œผ๋กœ Stream์„ ์ƒ์„ฑํ•˜์—ฌ ์ ‘๊ทผํ•ด์•ผ ํ•œ๋‹ค.

OutputStream์˜ ๊ฒฝ์šฐ Flushable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, ์ด๋Š” OutputStream์„ ํ™•์žฅํ•˜๋Š” ํ•˜์œ„ ํด๋ž˜์Šค๋“ค ์ž์ฒด์ ์œผ๋กœ Buffer ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ์•˜๋‹ค๊ฐ€ ํ•œ๋ฒˆ์— ์ €์žฅํ•˜๋Š”๋ฐ, ์ด๋ฅผ Flushable ์ธํ„ฐํŽ˜์ด์Šค์˜ flush() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด Buffer์™€ ๊ด€๊ณ„์—†์ด ๋ฐ”๋กœ ์ง€์ •๋œ ๋””๋ ‰ํ† ๋ฆฌ์— ์ €์žฅ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

Reader, Writer

Stream์€ byte์™€ byte[]์„ ์ฝ๊ณ  ์“ฐ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค๋ฉด, Reader์™€ Writer๋Š” char ํƒ€์ž… ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ  ์“ฐ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ํด๋ž˜์Šค์ด๋‹ค.


์ฐธ๊ณ  ์‚ฌ์ดํŠธ

 

[ 13์ฃผ์ฐจ ] Java์˜ I/O (feat. Stream, Buffer, Channel)

๋ชฉ์ฐจ ์ŠคํŠธ๋ฆผ(Stream), ๋ฒ„ํผ(Buffer), ์ฑ„๋„(Channel) ๊ธฐ๋ฐ˜์˜ I/O InputStream, OutputStream Byte์™€ Character ์ŠคํŠธ๋ฆผ ํ‘œ์ค€์ŠคํŠธ๋ฆผ(System.in, System.out, System.err) ํŒŒ์ผ ์ฝ๊ณ  ์“ฐ๊ธฐ ๊ฐœ์š” I/O๋ž€ Input/Output์œผ๋กœ..

javanitto.tistory.com

 

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

F018 - Serializable (์ง๋ ฌํ™”, ์—ญ์ง๋ ฌํ™”)  (0) 2021.06.16
F017 - Blocking, Non-Blocking / Synchronous, Asynchronous  (2) 2021.06.15
F015 - Set (์ž‘์„ฑ ์ค‘)  (0) 2021.05.31
F014 - List (ArrayList, LinkedList, Vector, Stack)  (0) 2021.05.30
F013 - Generic  (0) 2021.05.26