"์๋
ํ์ธ์! ์ค๋ ์์
์์๋ Java( Java I/O )์ ์
๋ ฅ ๋ฐ ์ถ๋ ฅ ์คํธ๋ฆผ์ ๋ํ ๋ํ๋ฅผ ๊ณ์ํ ๊ฒ์
๋๋ค. ์ด ์ฃผ์ ์ ๋ํ ์ฒซ ๋ฒ์งธ ์์
๋ ์๋๊ณ ํ์คํ ๋ง์ง๋ง๋ ์๋ ๊ฒ์
๋๋ค :) ์๋ฐ ์ธ์ด๋ I/O๋ก ์์
ํ ์ ์๋ ๋ง์ ๋ฐฉ๋ฒ์ ์ ๊ณตํฉ๋๋ค. ์ด ๊ธฐ๋ฅ์ ๊ตฌํํ๋ ํด๋์ค๊ฐ ์๋นํ ๋ง๊ธฐ ๋๋ฌธ์ ์ฒ์๋ถํฐ ํผ๋ํ์ง ์๋๋ก ์ฌ๋ฌ ๋ ์จ์ผ๋ก ๋๋์์ต๋๋ค. :) ์์
์์ ์ฐ๋ฆฌ๋
BufferedReader
, ๋ฐ ์ถ์ ํด๋์ค์ ์ฌ๋ฌ ํ์ ํด๋์ค์ ๋ํด ๋ค๋ฃจ์์ต๋๋ค . ์ค๋ ์ฐ๋ฆฌ๋ , , ์โฏ ์ธ ๊ฐ์ง ์๋ก์ด ํด๋์ค InputStream
๋ฅผ ๊ณ ๋ คํ ๊ฒ์
๋๋ค . OutputStream
FileInputStream
โฏFileOutputStream
BufferedInputStream
FileOutputStream ํด๋์ค
ํด๋์ค ์ ์ฃผ์ ๋ชฉ์ ์FileOutputStream
ํ์ผ์ ๋ฐ์ดํธ๋ฅผ ์ฐ๋ ๊ฒ์
๋๋ค. ๋ณต์กํ ๊ฒ์ ์์ต๋๋ค :)๋ ์ถ์ ํด๋์ค FileOutputStream
์ ๊ตฌํ ์ค ํ๋์
๋๋ค OutputStream
. ์์ฑ์์์ ์ด ํด๋์ค์ ๊ฐ์ฒด๋ ๋์ ํ์ผ(๋ฐ์ดํธ๋ฅผ ๊ธฐ๋กํด์ผ ํ๋ ์์น) ๋๋ ๊ฐ์ฒด์ ๋ํ ๊ฒฝ๋ก๋ฅผ ์ฌ์ฉํฉ๋๋ค File
. ๊ฐ๊ฐ์ ์๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
public class Main {
public static void main(String[] args) throws IOException {
File file = new File("C:\\Users\\Username\\Desktop\\test.txt");
FileOutputStream fileOutputStream = new FileOutputStream(file);
String greetings = "Hi! Welcome to CodeGym โ The best site for would-be programmers!";
fileOutputStream.write(greetings.getBytes());
fileOutputStream.close();
}
}
๊ฐ์ฒด๋ฅผ ๋ง๋ค ๋ File
์ํ๋ ๊ฒฝ๋ก๋ฅผ ์์ฑ์์ ์ ๋ฌํ์ต๋๋ค. ๋ฏธ๋ฆฌ ์์ฑํ ํ์๋ ์์ต๋๋ค. ์กด์ฌํ์ง ์์ผ๋ฉด ํ๋ก๊ทธ๋จ์ด ์์ฑํฉ๋๋ค. ์ถ๊ฐ ๊ฐ์ฒด๋ฅผ ์์ฑํ์ง ์๊ณ ๊ฒฝ๋ก์ ํจ๊ป ๋ฌธ์์ด์ ์ ๋ฌํ๊ธฐ๋ง ํ๋ฉด ์ป์ ์ ์์ต๋๋ค.
public class Main {
public static void main(String[] args) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Username\\Desktop\\test.txt");
String greetings = "Hi! Welcome to CodeGym โ The best site for would-be programmers!";
fileOutputStream.write(greetings.getBytes());
fileOutputStream.close();
}
}
๋ ๊ฒฝ์ฐ ๋ชจ๋ ๊ฒฐ๊ณผ๋ ๋์ผํฉ๋๋ค.โฏํ์ผ์ ์ด๊ณ ๋ค์์ ๋ณผ ์ ์์ต๋๋ค.
Hi! Welcome to CodeGym โ The best site for would-be programmers!
๊ทธ๋ฌ๋ ์ฌ๊ธฐ์๋ ํ ๊ฐ์ง ๋์์ค๊ฐ ์์ต๋๋ค. ์ ์์ ์ ์ฝ๋๋ฅผ ์ฐ์์ผ๋ก ์ฌ๋ฌ ๋ฒ ์คํํด ๋ณด์ญ์์ค. ๊ทธ๋ฐ ๋ค์ ํ์ผ์ ๋ณด๊ณ ๋ค์ ์ง๋ฌธ์ ๋ตํ์ญ์์ค. ํ์ผ์ ๋ช ์ค์ด ์์ต๋๊น? ๋ฑ ํ๋๋ง. ํ์ง๋ง ์ฝ๋๋ฅผ ์ฌ๋ฌ ๋ฒ ์คํํ์ต๋๋ค. ๋งค๋ฒ ๋ฐ์ดํฐ๋ฅผ ๋ฎ์ด์ฐ๋ ๊ฒ์ผ๋ก ๋ํ๋ฌ์ต๋๋ค. ์ด์ ๋ฐ์ดํฐ๋ ์ ๋ฐ์ดํฐ๋ก ๋์ฒด๋ฉ๋๋ค. ๊ทธ๊ฒ์ด ์ฐ๋ฆฌ์๊ฒ ์ ํฉํ์ง ์๊ณ ํ์ผ์ ์์ฐจ์ ์ผ๋ก ์์ฑํด์ผ ํ๋ ๊ฒฝ์ฐ ์ด๋ป๊ฒ ํด์ผ ํฉ๋๊น? ์ธ์ฌ๋ง์ ํ์ผ์ ์ฐ์์ผ๋ก ์ธ ๋ฒ ์ฐ๊ณ ์ถ๋ค๋ฉด ์ด๋ป๊ฒ ํด์ผ ํ ๊น์? ๋ชจ๋ ๋งค์ฐ ๊ฐ๋จํฉ๋๋ค. ์ธ์ด๋ ๊ฐ ๊ฒฝ์ฐ์ ์ด๋ค ํ๋์ด ํ์ํ์ง ์ ์ ์๊ธฐ ๋๋ฌธ์ FileOutputStream
๊ตฌ์ฑ์๋ ์ถ๊ฐ ๋งค๊ฐ๋ณ์๋ฅผ ์ทจํ ์ ์์ต๋๋ค.boolean append
. ๊ฐ์ด true์ด๋ฉด ๋ฐ์ดํฐ๊ฐ ํ์ผ ๋์ ๊ธฐ๋ก๋ฉ๋๋ค. false์ด๋ฉด(๊ธฐ๋ณธ๊ฐ์ false์) ์ด์ ๋ฐ์ดํฐ๊ฐ ๋ชจ๋ ์ง์์ง๊ณ ์ ๋ฐ์ดํฐ๋ก ๋์ฒด๋ฉ๋๋ค. ์์ ๋ ์ฝ๋๋ฅผ ์ธ ๋ฒ ์คํํ์ฌ ์ด๋ฅผ ํ์ธํฉ๋๋ค.
public class Main {
public static void main(String[] args) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Username\\Desktop\\test.txt", true);
String greetings = "Hi! Welcome to CodeGym โ The best site for would-be programmers!\r\n";
fileOutputStream.write(greetings.getBytes());
fileOutputStream.close();
}
}
ํ์ผ ๋ด์ฉ:
Hi! Welcome to CodeGym โ The best site for would-be programmers!
Hi! Welcome to CodeGym โ The best site for would-be programmers!
Hi! Welcome to CodeGym โ The best site for would-be programmers!
์ด์ ๋ค๋ฆ
๋๋ค! I/O ํด๋์ค๋ฅผ ์ฌ์ฉํ ๋ ์ด ๊ธฐ๋ฅ์ ์์ง ๋ง์ญ์์ค. ๋ด ๋ฐ์ดํฐ๊ฐ ํ์ผ์์ ์ด๋ป๊ฒ ์ฌ๋ผ์ง๋์ง ์ดํดํ๋ ค๊ณ ๋ช ์๊ฐ ๋์ ๋จธ๋ฆฌ๋ฅผ ์ฅ์ด์ง๊ณ ์์
์ ๋ช ์๊ฐ์ ๋ณด๋๋ ๋๊ฐ ์์์ต๋๋ค :) ๋ฌผ๋ก ๋ค๋ฅธ I/O ์์
๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ์ด ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ ๊ฒ์ ์์ง close()
๋ง์ธ์ ๋ฆฌ์์ค๋ฅผ ํ๋ณดํฉ๋๋ค.
FileInputStream ํด๋์ค
์๋FileInputStream
ํ์ผ์์ ๋ฐ์ดํธ๋ฅผ ์ฝ๋ ๋ฐ๋ ๋ชฉ์ ์ด ์์ต๋๋ค. FileOutputStream
์์ ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก OutputStream
์ด ํด๋์ค๋ InputStream
์ถ์ ํด๋์ค์์ ํ์๋ฉ๋๋ค. " test.txt " ํ์ผ ์ ๋ช ์ค์ ํ
์คํธ๋ฅผ ์์ฑํฉ๋๋ค .
"So close no matter how far
Couldn't be much more from the heart
Forever trusting who we are
And nothing else matters"
๋ค์์ ๋ค์์ ์ฌ์ฉํ์ฌ ํ์ผ์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ๋ ๋ชจ์ต์
๋๋ค FileInputStream
.
public class Main {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("C:\\Users\\Username\\Desktop\\test.txt");
int i;
while((i=fileInputStream.read())!= -1){
System.out.print((char)i);
}
}
}
ํ์ผ์์ ํ ๋ฐ์ดํธ๋ฅผ ์ฝ๊ณ ์ฝ์ ๋ฐ์ดํธ๋ฅผ ๋ฌธ์๋ก ๋ณํํ์ฌ ์ฝ์์ ํ์ํฉ๋๋ค. ๋ค์์ ์ฝ์ ์ถ๋ ฅ์
๋๋ค.
So close no matter how far
Couldn't be much more from the heart
Forever trusting who we are
And nothing else matters
BufferedInputStream ํด๋์ค
์ง๋ ์์ ์์ ์ป์ ์ง์์ ๋ฐํ์ผ๋ก ํด๋์ค๊ฐ ํ์ํ ์ด์BufferedInputStream
์ ๋น๊ตํ์ ๋ ์ด๋ค ์ด์ ์ด ์๋์ง ์ฝ๊ฒ ๋งํ ์ ์์ ๊ฒ ๊ฐ์ต๋๋ค FileInputStream
:) ๋ฒํผ๋ง๋ ์คํธ๋ฆผ์ ์ด๋ฏธ ์ ํ์ผ๋ฏ๋ก ๊ณ์ ์ฝ๊ธฐ ์ ์ ์ถ์ธก(๋๋ ๊ธฐ์ต)ํด ๋ณด์ญ์์ค :) ๋ฒํผ๋ง๋ ์คํธ๋ฆผ์ ์ฃผ๋ก I/O๋ฅผ ์ต์ ํํ๋ ๋ฐ ํ์ํฉ๋๋ค. ํ์ผ์์ ์ฝ๊ธฐ์ ๊ฐ์ด ๋ฐ์ดํฐ ์์ค์ ์ก์ธ์คํ๋ ๊ฒ์ ์ฑ๋ฅ ์ธก๋ฉด์์ ๋น์ฉ์ด ๋ง์ด ๋๋ ์์
์ด๋ฉฐ ๊ฐ ๋ฐ์ดํธ๋ฅผ ์ฝ๊ธฐ ์ํด ํ์ผ์ ์ก์ธ์คํ๋ ๊ฒ์ ๋ญ๋น์
๋๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ BufferedInputStream
ํ ๋ฒ์ ํ ๋ฐ์ดํธ๊ฐ ์๋๋ผ ๋ธ๋ก ๋จ์๋ก ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ณ ์์๋ก ํน์ ๋ฒํผ์ ์ ์ฅํฉ๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด ํ์ผ์ ์ก์ธ์คํ๋ ํ์๋ฅผ ์ค์ฌ ํ๋ก๊ทธ๋จ์ ์ต์ ํํ ์ ์์ต๋๋ค. ์ด๊ฒ์ด ์ด๋ป๊ฒ ๋ณด์ด๋์ง ๋ด
์๋ค:
public class Main {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("C:\\Users\\Username\\Desktop\\test.txt");
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream, 200);
int i;
while((i = bufferedInputStream.read())!= -1){
System.out.print((char)i);
}
}
}
์ฌ๊ธฐ์ ์ฐ๋ฆฌ๋ BufferedInputStream
๊ฐ์ฒด๋ฅผ ๋ง๋ค์์ต๋๋ค. ๊ทธ ์์ฑ์๋ ํด๋์ค์ ์ธ์คํด์ค InputStream
๋ ๊ทธ ์์ ์ค ํ๋๋ฅผ ์ทจํ๋ฏ๋ก ๊ทธ๋ ๊ฒ FileInputStream
ํ ๊ฒ์
๋๋ค. ์ถ๊ฐ ์ธ์๋ก ๋ฒํผ ํฌ๊ธฐ(๋ฐ์ดํธ)๋ฅผ ์ฌ์ฉํฉ๋๋ค. ์ด ์ธ์ ๋๋ถ์ ํ์ผ์์ ํ ๋ฒ์ ํ ๋ฐ์ดํธ๊ฐ ์๋๋ผ ํ ๋ฒ์ 200๋ฐ์ดํธ์ฉ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ ์ ์์ต๋๋ค! ํ์ผ ์ก์ธ์ค ํ์๋ฅผ ์ผ๋ง๋ ์ค์๋์ง ์์ํด ๋ณด์ญ์์ค. ์ฑ๋ฅ์ ๋น๊ตํ๋ ค๋ฉด ํฐ ํ
์คํธ ํ์ผ(๋ช ๋ฉ๊ฐ๋ฐ์ดํธ์ ํ
์คํธ)์ ๊ฐ์ ธ์ FileInputStream
๋ฐ ๋ฅผ ์ฌ์ฉํ์ฌ ์ฝ๊ณ ์ฝ์์ ์ถ๋ ฅํ๋ ๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ(๋ฐ๋ฆฌ์ด)์ ๋น๊ตํ ์ ์์ต๋๋ค BufferedInputStream
. ๋ค์์ ๋ ์ต์
์ ๋ชจ๋ ๋ณด์ฌ์ฃผ๋ ์ฝ๋์
๋๋ค.
public class Main {
public static void main(String[] args) throws IOException {
Date date = new Date();
FileInputStream fileInputStream = new FileInputStream("C:\\Users\\Username\\Desktop\\textBook.rtf");
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
int i;
while((i = bufferedInputStream.read())!= -1){
System.out.print((char)i);
}
Date date1 = new Date();
System.out.println((date1.getTime() - date.getTime()));
}
}
public class Main {
public static void main(String[] args) throws IOException {
Date date = new Date();
FileInputStream fileInputStream = new FileInputStream("C:\\Users\\Username\\Desktop\\26951280.rtf");
int i;
while((i = fileInputStream.read())!= -1){
System.out.print((char)i);
}
Date date1 = new Date();
System.out.println((date1.getTime() - date.getTime()));
}
}
๋ด ์ปดํจํฐ์์ 1.5MB ํ์ผ์ ์ฝ์ ๋ FileInputStream
~3500๋ฐ๋ฆฌ์ด ์์ ์์
์ ์๋ฃํ์ง๋ง BufferedInputStream
~1700๋ฐ๋ฆฌ์ด ๋ง์ ์ฒ๋ฆฌํ์ต๋๋ค. ๋ณด์๋ค์ํผ ๋ฒํผ๋ง๋ ์คํธ๋ฆผ์ ์์
์ ์ต์ ํํ์ฌ ์ ๋ฐ์ผ๋ก ์ค์์ต๋๋ค! :) I/O ์์
์ ๊ณ์ ๊ณต๋ถํ๊ฒ ์ต๋๋ค. ๊ณง ๋ต๊ฒ ์ต๋๋ค!
GO TO FULL VERSION