๊ตฌ์ฒด์ ์ธ ๋ด์ฉ์ ๋ค์ด๊ฐ๊ธฐ์ ์์ ๊ธฐ๋ณธ์ ์ธ ๋จ์ด 3๊ฐ๋ฅผ ์ดํด๋ณด์.
I/O๋ Input/Ouput์ ์ฝ์๋ก ์ ๋ ฅ๊ณผ ์ถ๋ ฅ์ ๋ํ๋ธ๋ค. ๋ ธ๋(Node)๋ '๋ฐ์ดํฐ์ ์ถ๋ฐ์ง์ ๋ชฉ์ ์ง' ๋๋ '์ ๋ ฅ๊ณผ ์ถ๋ ฅ์ ๋๋จ'์ด๋ค. ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๊ธฐ ๋ฐ์ดํฐ๋ฅผ ์ด๋ฐํ๋ ๋ฐ ์ฌ์ฉ๋๋ ์ฐ๊ฒฐ ํต๋ก๊ฐ ํ์ํ๋ฐ ๊ทธ๊ฒ์ด ์คํธ๋ฆผ(Stream)์ด๋ค.
์ด๋ฒ ์ฃผ์ ์์๋ ์ ์ถ๋ ฅ์ ๊ฐ๋ฅํ๊ฒ ํ๋ ๋ ธ๋ ์คํธ๋ฆผ, ์ด๋ฅผ ๋์์ฃผ๋ ๋ณด์กฐ ์คํธ๋ฆผ, ๊ทธ๋ฆฌ๊ณ ์ง๋ ฌํ(Serialization)๋ฅผ ์์๋ณด์.
1. ๋ ธ๋ ์คํธ๋ฆผ ๐ด
๋ ธ๋ ์คํธ๋ฆผ์ด๋ ๋ ธ๋์ ์ฐ๊ฒฐ๋ ์คํธ๋ฆผ์ด๋ค. ๋ค์ํ ๋ ธ๋ ์คํธ๋ฆผ๋ค์ด ์๋๋ฐ 3๊ฐ์ง ์กฐ๊ฑด์ผ๋ก ๋ถ๋ฅ๋ฅผ ํ์ฌ ๊ทธ๊ฒ์ ํด๋นํ๋ ๋ ธ๋์คํธ๋ฆผ์ ์ฌ์ฉํ๋ฉด ๋๋ค. 3๊ฐ์ง ๋ถ๋ฅ๋ ์๋์ ๊ฐ๋ค.
- ๋ฐ์ดํฐ ํ์
์ ์กํ ๋ฐ์ดํฐ ํ์ ์ด Byte์ธ์ง ์๋๋ฉด Char์ธ์ง - ๋ฐ์ดํฐ ์ด๋๋ฐฉํฅ
๋ฐ์ดํฐ๋ฅผ ์ ๋ ฅ๋ฐ๋ ๊ฒฝ์ฐ์ ์ถ๋ ฅํ๋ ๊ฒฝ์ฐ - ๋
ธ๋ ํ์
ํค๋ณด๋, ๋ชจ๋ํฐ, File, ByteArray, Pipe, CharArray, String
์ต์ข ๋ ธ๋ ์คํธ๋ฆผ์ ๊ฐ์๊ฐ ์๊ฐ๋ณด๋ค ๋ง๋ค. ํ์ง๋ง ์ด๋ค์ 'InputStream, OutputStream', 'Reader, Writer'์ ์์ํด๋์ค์ด๋ฏ๋ก InputStream, OutputStream, Reader, Writer๋ง ์ดํด๋ณด๋๋ก ํ์. ๋ํ, FileInputStream๊ณผ FileOutputStream์ ์์ฃผ ์ฌ์ฉ๋๋ฏ๋ก ์ด๊ฒ๋ ๊ฐ์ด ์ดํด๋ณด์.
InputStream๊ณผ OutputStream โพ
[InputStream]
๋ฉ์๋๋ช | ์ค๋ช |
abstract int read() | 1byte๋ฅผ ์ฝ์ด์จ๋ค. ๋ ์ด์ ์ฝ์ด ์ฌ ๋ฐ์ดํฐ๊ฐ ์์ผ๋ฉด -1์ ๋ฐํํ๋ค. |
int read(byte[] b) | ๋ฐฐ์ด b์ ํฌ๊ธฐ๋งํผ ์ฝ์ด์ ๋ฐฐ์ด์ ์ฑ์ฐ๊ณ ์ฝ์ด ์จ ๋ฐ์ดํฐ์ ์๋ฅผ ๋ฐํํ๋ค. ๋ ์ด์ ์ฝ์ ๊ฐ์ด ์์ผ๋ฉด 0์ ๋ฐํํ๋ค. |
int read(byte[] b, int off, int len) | ์ต๋ len๊ฐ์ byte๋ฅผ ์ฝ์ด์ ๋ฐฐ์ด b์ ์ง์ ๋ ์์น(off)๋ถํฐ ์ ์ฅํ๋ค. off + len์ b์ ํฌ๊ธฐ ์ดํ์ฌ์ผ ํ๋ค. |
[OutputStream]
๋ฉ์๋๋ช | ์ค๋ช |
abstract void write(int b) | ์ฃผ์ด์ง ๊ฐ์ ์ถ๋ ฅ์์ค์ ์ด๋ค. |
void write(byte[] b) | ์ฃผ์ด์ง ๋ฐฐ์ด b์ ์ ์ฅ๋ ๋ชจ๋ ๋ด์ฉ์ ์ถ๋ ฅ์์ค์ ์ด๋ค. |
void write(byte[] b, int off, int len) | ์ฃผ์ด์ง ๋ฐฐ์ด b์ ์ ์ฅ๋ ๋ด์ฉ ์ค์์ off๋ฒ์งธ๋ถํฐ len๊ฐ ๋งํผ๋ง ์ฝ์ด์ ์ถ๋ ฅ์์ค์ ์ด๋ค. |
public static void main(String[] args) {
byte[] inSrc = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
byte[] outSrc = null;
byte[] temp = new byte[10];
ByteArrayInputStream input = new ByteArrayInputStream(inSrc);
ByteArrayOutputStream output = new ByteArrayOutputStream();
System.out.println("๊ธฐ์กด temp : " + Arrays.toString(temp));
input.read(temp, 0, temp.length); // ์ฝ์ด์จ ๋ฐ์ดํฐ์ธ inSrc๋ฅผ temp์ ๋ด๋๋ค.
System.out.println("์
๋ ฅ ํ temp : " + Arrays.toString(temp));
output.write(temp, 5, 5); // temp[5]๋ถํฐ 5๊ฐ์ ๋ฐ์ดํฐ๋ฅผ writeํ๋ค.
outSrc = output.toByteArray();
System.out.println("์ถ๋ ฅ ํ outSrc : " + Arrays.toString(outSrc));
}
// ๊ธฐ์กด temp : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
// ์
๋ ฅ ํ temp : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
// ์ถ๋ ฅ ํ outSrc : [5, 6, 7, 8, 9]
๊ธฐ๋ณธ์ ์ธ ์ฌ์ฉ๋ฒ์ ์คํธ๋ฆผ์ ์ข ๋ฅ๋ง ๋ค๋ฅผ ๋ฟ ์ฝ๊ณ ์ฐ๋ ๋ฐฉ๋ฒ์ ๋์ผํ๋ฏ๋ก ์ถ๊ฐ์ ์ธ ์ฝ๋๋ ์๋ตํ๋ค.
Reader์ Writer ๐
[Reader]
๋ฉ์๋ | ์ค๋ช |
int read() | ์
๋ ฅ์์ค๋ก๋ถํฐ ํ๋์ ๋ฌธ์๋ฅผ ์ฝ์ด์จ๋ค. ๋ ์ด์ ์ฝ์ ๊ฐ์ด ์์ผ๋ฉด -1์ ๋ฐํํ๋ค. |
int read(char[] c) | ์
๋ ฅ์์ค๋ก๋ถํฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋ฐฐ์ด c์ ํฌ๊ธฐ๋งํผ ์ฝ์ด์ ๋ฐฐ์ด c์ ์ ์ฅํ๋ค. ๋ ์ด์ ์ฝ์ ๊ฐ์ด ์์ผ๋ฉด 0์ด ๋ฐํ๋๋ค. |
abstract int read(char[] c, int off, int len) | ์
๋ ฅ์์ค๋ก๋ถํฐ ์ต๋ len๊ฐ์ ๋ฌธ์๋ฅผ ์ฝ์ด์ ๋ฐฐ์ด c์ ์ง์ ๋๋ ์์น(off)๋ถํฐ ์ฝ์ ๋งํผ ์ ์ฅํ๋ค. off + len์ c์ ํฌ๊ธฐ ์ดํ์ฌ์ผ ํ๋ค. |
int read(CharBuffer target) | ์ ๋ ฅ์์ค๋ก๋ถํฐ ์ฝ์ด์ ๋ฌธ์๋ฒํผ(target)์ ์ ์ฅํ๋ค. |
abstract void close() | ์คํธ๋ฆผ์ ์ข ๋ฃํด์ ์์์ ๋ฐ๋ฉํ๋ค. |
[Writer]
๋ฉ์๋ | ์ค๋ช |
void write(int b) | ์ฃผ์ด์ง ๊ฐ์ ์ถ๋ ฅ์์ค์ ์ด๋ค. |
void write(char[] c) | ์ฃผ์ด์ง ๋ฐฐ์ด c์ ์ ์ฅ๋ ๋ชจ๋ ๋ด์ฉ์ ์ถ๋ ฅ์์ค์ ์ด๋ค. |
abstract void write(char[] c, int off, int len) | ์ฃผ์ด์ง ๋ฐฐ์ด c์ ์ ์ฅ๋ ๋ด์ฉ ์ค์์ off๋ฒ์งธ๋ถํฐ len๊ธธ์ด๋งํผ๋ง ์ถ๋ ฅ์์ค์ ์ด๋ค. |
void write(String str) | ์ฃผ์ด์ง ๋ฌธ์์ด(str)์ ์ถ๋ ฅ์์ค์ ์ด๋ค. |
void write(String str, int off, int len) | ์ฃผ์ด์ง ๋ฌธ์์ด(str)์ off๋ฒ์งธ ๋ฌธ์๋ถํฐ len๊ฐ ๋งํผ์ ๋ฌธ์์ด์ ์ถ๋ ฅ์์ค์ ์ด๋ค. |
Writer append(c) | ์ง์ ๋ ๋ฌธ์๋ฅผ ์ถ๋ ฅ์์ค์ ์ถ๋ ฅํ๋ค. |
Writer append(CharSequence c) | c๋ฅผ ์ถ๋ ฅ์์ค์ ์ถ๋ ฅํ๋ค. |
Writer append(CharSequence c, int start, int end) | c์ ์ผ๋ถ๋ฅผ ์ถ๋ ฅ์์ค์ ์ถ๋ ฅํ๋ค. |
abstract void close() | ์คํธ๋ฆผ์ ์ข ๋ฃํด์ ์์์ ๋ฐ๋ฉํ๋ค. |
abstract void flush() | ๋ฒํผ๊ฐ ์๋ ์คํธ๋ฆผ์์ ๋ฒํผ์ ๋ด์ฉ์ ์ถ๋ ฅํ๊ณ ๋ฒํผ๋ฅผ ๋น์ด๋ค. |
FileInputStream๊ณผ FileOutputStream ๐
[FileInputStream & FileOutputStream]
์์ฑ์ | ์ค๋ช |
FileInputStream(String name) | name์ ๊ฐ์ง ์ค์ ํ์ผ๊ณผ ์ฐ๊ฒฐ๋ FileInputStream์ ์์ฑํ๋ค. |
FileOutputStream(String name) | name์ ๊ฐ์ง ์ค์ ํ์ผ๊ณผ ์ฐ๊ฒฐ๋ FileOutputStream์ ์์ฑํ๋ค. |
FileOutputStream(String name, boolean append) | name์ ๊ฐ์ง ์ค์ ํ์ผใน๊ณผ ์ฐ๊ฒฐ๋ FileOutputStream์ ์์ฑํ๋ค. ๋ง์ฝ append๊ฐ true์ด๋ฉด ๊ธฐ์กด ํ์ผ์ ์ด์ด์ ์ฐ๊ณ , false์ด๋ฉด ๊ธฐ์กด์ ํ์ผ ๋ด์ฉ์ ๋ฎ์ด์ฐ๊ฒ ๋๋ค. |
File src = new File("C:\\src.txt");
File target = new File("C:\\new\\target.txt");
byte[] buffer = new byte[10];
try(InputStream input = new FileInputStream(src);
OutputStream output = new FileOutputStream(target)){
int read = -1;
while((read = input.read(buffer)) > 0) {
output.write(buffer, 0, read);
}
} catch(IOException e) {
e.printStackTrace();
}
ํ์ผ์ ์ฌ์ฉํ ๋ ๊ฒฝ๋ก๋ฅผ ์ง์ ํด์ค์ผ ํ๋ ๊ฒฝ์ฐ๋ค์ด ์๋ค. OS๋ง๋ค ๊ฒฝ๋ก(path) ๊ตฌ๋ถ์๊ฐ ๋ค๋ฅด๋ฏ๋ก ์์ ์ OS ๊ฒฝ๋ก ๊ตฌ๋ถ์๋ฅผ ์ฐ๋ ๊ฒ ์๋๋ผ ๊ฒฝ๋ก์ ๊ด๋ จ๋ File์ ๋ฉค๋ฒ๋ณ์๋ฅผ ์ฌ์ฉํ๋๋ก ํ์.
๋ฉค๋ฒ๋ณ์ | ์ค๋ช |
static String pathSeparator | OS์์ ์ฌ์ฉ๋๋ ๊ฒฝ๋ก ๊ตฌ๋ถ์ ์๋์ฐ = ";", ์ ๋์ค = ":" |
static char pathSeparatorChar | OS์์ ์ฌ์ฉ๋๋ ๊ฒฝ๋ก ๊ตฌ๋ถ์ ์๋์ฐ = ';', ์ ๋์ค = ':' |
static String separator | OS์์ ์ฌ์ฉํ๋ ์ด๋ฆ ๊ตฌ๋ถ์ ์๋์ฐ = "\", ์ ๋์ค = "/" |
static char separatorChar | OS์์ ์ฌ์ฉํ๋ ์ด๋ฆ ๊ตฌ๋ถ์ ์๋์ฐ = "\", ์ ๋์ค = "/" |
File target = new File("C:" + File.separator + "Amenable" + File.separator + "test.txt");
2. ๋ณด์กฐ ์คํธ๋ฆผ ๐ต
๋ณด์กฐ ์คํธ๋ฆผ์ ๋ง ๊ทธ๋๋ก ๋ณด์กฐ ๊ธฐ๋ฅ์ ์ถ๊ฐํ๋ ์คํธ๋ฆผ์ด๋ค. ์์ฒด์ ์ผ๋ก ์ ์ถ๋ ฅ์ ์ํํ ์ ์๊ธฐ ๋๋ฌธ์ ์์์ ๋ฐฐ์ด ๋ ธ๋์คํธ๋ฆผ์ด ํ์ํ๋ค. ํ์์ ๋ฐ๋ผ์ ์ฌ๋ฌ ๋ณด์กฐ ์คํธ๋ฆผ์ ์ฐ๊ฒฐํด์ ์ฌ์ฉํ ์๋ ์๋๋ฐ ์ด๋ฅธ ์คํธ๋ฆผ ์ฒด์ด๋(Stream Chaining)์ด๋ผ๊ณ ํ๋ค.
๊ธฐ๋ฅ | byte ๊ธฐ๋ฐ | char ๊ธฐ๋ฐ |
byte ์คํธ๋ฆผ์ char ์คํธ๋ฆผ์ผ๋ก ๋ณํ | InputStreamReader OutputStreamWriter |
|
๋ฒํผ๋ง์ ํตํ ์๋ ํฅ์ | BufferedInputStream BufferedOutputStream |
BufferedReader BufferedWriter |
๊ฐ์ฒด ์ ์ก | ObjectInputStream ObjectOutputStream |
๋ฒํผ๋ฅผ ์ด์ฉํ๋ฉด ์ ์ถ๋ ฅ์ ํจ์จ์ด ๋น๊ตํ ์ ์์ ์ ๋๋ก ์ข์์ง๊ธฐ ๋๋ฌธ์ ์ฌ์ฉํ๋ ๊ฒ์ ์ถ์ฒํ๋ค.
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
ํด๋น ๊ธ์ ๋จ๊ถ ์ฑ๋์ Java์ ์ ์์ ์ฝ๊ณ ์์ฑํ ๊ฒ์ ๋๋ค.
'๐ JAVA > ์ฃผ์ ๊ฐ๋ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Parsing(ํ์ฑ) - CSV, XML, JSON (0) | 2023.01.30 |
---|---|
์ ์ถ๋ ฅ(I/O)(2) - ์ง๋ ฌํ(Serialization) (2) | 2023.01.29 |
๋๋ค์(Lambda Expression) - (2) (0) | 2023.01.29 |
๋๋ค์(Lambda Expression) - (1) (0) | 2023.01.29 |
Comparable๊ณผ Comparator (0) | 2023.01.28 |