CodeGym /Java Course /์ž๋ฐ” ์ปฌ๋ ‰์…˜ /๋…์ž, ์ž‘๊ฐ€

๋…์ž, ์ž‘๊ฐ€

์ž๋ฐ” ์ปฌ๋ ‰์…˜
๋ ˆ๋ฒจ 1 , ๋ ˆ์Šจ 7
์‚ฌ์šฉ ๊ฐ€๋Šฅ

"์•ˆ๋…•, ์•„๋ฏธ๊ณ , ๋‹ค์‹œ ํ•œ ๋ฒˆ."

"์•ˆ๋…•ํ•˜์„ธ์š”, Rishi. ์˜ค๋Š˜ ์ˆ˜์—…์€ ๋ฌด์—‡์— ๊ด€ํ•œ ๊ฒƒ์ž…๋‹ˆ๊นŒ?"

"์˜ค๋Š˜์€ ๋…์ž์™€ ์ž‘๊ฐ€์— ๋Œ€ํ•ด ์ด์•ผ๊ธฐํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค."

"ํ•˜์ง€๋งŒ Rishi, ๋‚˜๋Š” ์ด๋ฏธ ๊ทธ๋“ค์— ๋Œ€ํ•ด ๊ฑฐ์˜ ๋ชจ๋“  ๊ฒƒ์„ ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค!"

"๋ฐ˜๋ณต์€ ๋ฐฐ์›€์˜ ์–ด๋จธ๋‹ˆ๋‹ค."

"์Œ. ์•Œ์•˜์–ด."

" Reader ๋ฐ Writer ๋Š” InputStream ๋ฐ OutputStream ๊ณผ ์œ ์‚ฌ ํ•˜์ง€๋งŒ ๋ฐ”์ดํŠธ๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž๋กœ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ๋•Œ๋กœ๋Š” ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ์ด๋ผ๊ณ  ํ•˜๋Š” InputStream  ๋ฐ  OutputStream ๊ณผ ๋‹ฌ๋ฆฌ  ๋ฌธ์ž ์ŠคํŠธ๋ฆผ์ด๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค."

"ํ•˜๋‚˜๋Š” ๋ฌธ์ž์šฉ์ด๊ณ  ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” ๋ฐ”์ดํŠธ์šฉ์ž…๋‹ˆ๋‹ค. ๊ธฐ์–ตํ•ฉ๋‹ˆ๋‹ค."

"๊ทธ๋ฟ๋งŒ์ด ์•„๋‹™๋‹ˆ๋‹ค. ์ด ํด๋ž˜์Šค๋Š” ํ…์ŠคํŠธ ๋ฐ ๋ฌธ์ž์—ด๊ณผ ํ•จ๊ป˜ ์ž‘๋™ํ•˜๋„๋ก ํŠน๋ณ„ํžˆ ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์˜ค๋Š˜ ์šฐ๋ฆฌ๋Š” ์ด๋Ÿฌํ•œ ์ถ”์ƒ ํด๋ž˜์Šค์˜ ๋‘ ๊ฐ€์ง€ ํด๋ž˜์‹ ๊ตฌํ˜„์ธ FileReader ๋ฐ FileWriter๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค ."

"๋‹ค์Œ์€ FileReader ํด๋ž˜์Šค ์˜ ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค ."

๋ฐฉ๋ฒ• ์„ค๋ช…
int read() ์ŠคํŠธ๋ฆผ์—์„œ ํ•œ ๋ฌธ์ž๋ฅผ ์ฝ๊ณ  ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
int read(char cbuf[], int offset, int length) ๋ฌธ์ž ๋ฐฐ์—ด์„ ์ฝ๊ณ  ์ฝ์€ ๋ฌธ์ž ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
boolean ready() ์ŠคํŠธ๋ฆผ์—์„œ ์ฝ์„ ์ˆ˜ ์žˆ์œผ๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
void close() ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์„ ๋‹ซ์Šต๋‹ˆ๋‹ค.
int read(java.nio.CharBuffer target) ๋ฌธ์ž ์ง‘ํ•ฉ์„ ๋ฒ„ํผ๋กœ ์ฝ์Šต๋‹ˆ๋‹ค.
int read(char cbuf[]) ๋ฌธ์ž ๋ฐฐ์—ด์„ ์ฝ์Šต๋‹ˆ๋‹ค.
long skip(long n) ์ŠคํŠธ๋ฆผ์—์„œ n์ž๋ฅผ ๊ฑด๋„ˆ๋œ๋‹ˆ๋‹ค.
String getEncoding() ์ŠคํŠธ๋ฆผ์˜ ํ˜„์žฌ ์ธ์ฝ”๋”ฉ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

"์Œ, ๊ฑฐ์˜ ๋‹ค ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ FileWriter๋Š” ์–ด๋–ป์Šต๋‹ˆ๊นŒ?"

๋ฐฉ๋ฒ• ์„ค๋ช…
void write(int c) ์ŠคํŠธ๋ฆผ์— ํ•œ ๋ฌธ์ž๋ฅผ ์”๋‹ˆ๋‹ค.
void write(char cbuf[], int off, int len) ์ŠคํŠธ๋ฆผ์— ๋ฌธ์ž ๋ฐฐ์—ด์„ ์”๋‹ˆ๋‹ค.
void write(char cbuf[]) ์ŠคํŠธ๋ฆผ์— ๋ฌธ์ž ๋ฐฐ์—ด์„ ์”๋‹ˆ๋‹ค.
void write(String str, int off, int len) ๋ฌธ์ž์—ด์˜ ์ผ๋ถ€๋ฅผ ์ŠคํŠธ๋ฆผ์— ์”๋‹ˆ๋‹ค.
void write(String str) ์ŠคํŠธ๋ฆผ์— ๋ฌธ์ž์—ด์„ ์”๋‹ˆ๋‹ค.
void flush() ๋ฉ”๋ชจ๋ฆฌ์— ์บ์‹œ๋œ ๋ชจ๋“  ๊ฒƒ์„ ๋””์Šคํฌ์— ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.
void close() ์ŠคํŠธ๋ฆผ์„ ๋‹ซ์Šต๋‹ˆ๋‹ค.
String getEncoding() ์ŠคํŠธ๋ฆผ์˜ ํ˜„์žฌ ์ธ์ฝ”๋”ฉ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

"๋‚˜๋„ ์•Œ์•„!"

"๋ฉ‹์ง€๋„ค์š”. ๊ทธ๋Ÿผ ํฅ๋ฏธ๋กœ์šด ์˜ˆ๋ฅผ ํ•˜๋‚˜ ๋ด…์‹œ๋‹ค. ๋‚˜์ค‘์— ๋””์—๊ณ ๊ฐ€ ๋‹น์‹ ์—๊ฒŒ ๋” ๋งŽ์€ ์ผ์„ ์ค„ ๊ฒƒ์ž…๋‹ˆ๋‹ค."

"ํŒŒ์ผ์„ ํ•œ ์ค„์”ฉ ์–ด๋–ป๊ฒŒ ์ฝ๋‚˜์š”? ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‚˜์š”?"

"์‰ฝ๊ฒŒ ๋ด:"

์•”ํ˜ธ
// Create a list for storing the lines
List<String> list = new ArrayList<String>();

// Open the file
File file = new File("c:/document.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));

// As long as the file isn't empty, read from it
while (reader.ready())
{
 list.add(reader.readLine());
}

// Close the file
reader.close();

"์Œ. ๋‚˜์˜์ง€ ์•Š์•„."

"๋‚˜์˜์ง€ ์•Š์ฃ ? ๋ชจ๋‘ ๋‹จ์ˆœํ•˜๊ณ  ์•„๋ฆ„๋‹ค์›Œ์š”. Rishi ์ธ์ •ํ•˜์„ธ์š”. ์ €๋Š” ์ด๋ฏธ I/O ์Šค๋ ˆ๋“œ์— ๋Œ€ํ•œ ๋›ฐ์–ด๋‚œ ์ˆ™๋‹ฌ๋ ฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ์ ์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?"

"์Œ, ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."

๋ฆฌ์‹œ์˜ ์ฝ”๋“œ
File file = new File("c:/document.txt");

List list = Files.readAllLines(file.toPath(), Charset.defaultCharset());

"ํ . ๊ทธ๊ฒŒ ๋” ์งง๋„ค์š”. ๊ทธ๋ฆฌ๊ณ  ์˜ค๋Š˜ ๋‹น์‹ ์€ ์ด ๋ชจ๋“  ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ €์—๊ฒŒ ๋งํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž‘์—…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ•์˜ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค, Rishi."

"ํ–‰์šด์„ ๋นŒ์–ด์š”, ์•„๋ฏธ๊ณ ."

์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION