Java์ PrintWriter ํด๋์ค๋ ๋ฌด์์ ๋๊น?
๋ค๋ฅธ OutputStream์ ์ฌ์ฉํ ์ ์๋๋ฐ ์ PrintWriter๋ฅผ ์ฌ์ฉํฉ๋๊น?
์ฝ์์์ ๋ฐ์ดํฐ๋ฅผ ์ธ์ํ๋ ๊ฐ์ฅ ์ผ๋ฐ์ ์ธ ๋ฐฉ๋ฒ์ System.out.print ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๋๋ค. ๊ทธ๋ฌ๋ PrintWriter ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ์ฌ ์ ์ญ ์์ฉ ํ๋ก๊ทธ๋จ์ ๊ฒ์ํ๋ ๋์ ์ง์ ๋ ๋ก์บ (์ง์ญ ํ์ค) ์ ๋ฐ๋ผ ํ์์ ์ฌ์ฉ์ ์ง์ ํ๋ ๊ฒ์ด ๋ ์ฝ์ต๋๋ค . ์ด ๊ฒ์๋ฌผ์ ๋ท๋ถ๋ถ์์ ์์คํ ์ ๋ฐ๋ผ Locale์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ์ดํด๋ณผ ์ ์์ต๋๋ค.PrintWriter ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ?
PrintWriter ๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด java.io.PrintWriter ํด๋์ค๋ฅผ ๊ฐ์ ธ์์ผ ํฉ๋๋ค . ๊ทธ๋ฐ ๋ค์ ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ ํ ํ์์ ๋ฐ๋ผ ์ฝ์์ด๋ ํ์ผ์ ์ฐ๊ธฐ ์ํด ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ฝ์๊ณผ ํ์ผ์ ๋ํด PrintWriter ํด๋์ค ๋ฅผ ์ด๊ธฐํํ๋ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค . ์ฌ๋ฌ ๋ค๋ฅธ ์์ฑ์๊ฐ ์์ต๋๋ค. ํ์ง๋ง ์ฌ๊ธฐ์๋ ์ฐ์ ๊ฐ์ฅ ๊ฐ๋จํ ๊ฒ์ ์๊ฐํ๊ฒ ์ต๋๋ค.PrintWriter๋ฅผ ์ฌ์ฉํ ์ฝ์ ์ถ๋ ฅ
๋ค์์ ์ฝ์์ ํ ์คํธ๋ฅผ ์ธ์ํ๋ PrintWrtier ๊ฐ์ฒด ์ ๋๋ค .
PrintWriter consoleOutput = new PrintWriter(System.out);
์ฌ๊ธฐ์ System.out ๊ฐ์ฒด๋ ์ฝ์์ ์ฐ๊ธฐ ์ํด ์์ฑ์์ ์ ๋ฌ๋ฉ๋๋ค.
PrintWriter๋ก ํ์ผ ์ถ๋ ฅ
๋ค์์ ํ์ผ์ ํ ์คํธ๋ฅผ ์ฐ๋ PrintWriter ๊ฐ์ฒด ์ ๋๋ค .
PrintWriter fileOutput = new PrintWriter("FileOutput.txt");
์ด ์์ฑ์๋ ๋ฌธ์์ด ์
๋ ฅ์ ํ์ผ ์ด๋ฆ์ผ๋ก ์ฌ์ฉํฉ๋๋ค. ์ง์ ๋ ์ด๋ฆ์ ํ์ผ์ ์์ฑํ๊ณ ๊ทธ ์์ ํ
์คํธ ๋ฐ์ดํฐ๋ฅผ ์๋๋ค.
PrintWriter ํด๋์ค์ ๋ฉ์๋
Java PrintWriter ํด๋์ค์๋ ์ฌ๋ฌ ํธ๋ฆฌํ ๋ฉ์๋๊ฐ ํจ๊ป ์ ๊ณต๋ฉ๋๋ค. ์ ๋๋ง์ผ๋ก๋ ์ผํค๊ธฐ๊ฐ ๋ ์ด๋ ต์ต๋๋ค. ๊ทธ๋ผ ํ๋ํ๋ ์๋ฅผ ๋ค์ด ๋ณด๊ฒ ์ต๋๋ค. ๊ทธ๋ค์ ๋ฌด์์ด๋ฉฐ ์ด๋ป๊ฒ ์ฝ๊ฒ ์ฌ์ฉํ ์ ์์ต๋๊น?์ 1
์ด ์์ ๋ ์ฝ์์์ ์ธ์ํ๊ธฐ ์ํด PrintWriter ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค .
import java.io.PrintWriter;
public class PrintWriterDemo {
public static void main(String[] args) throws Exception {
// by importing the java.io.PrintWriter class
PrintWriter consoleOutput = new PrintWriter(System.out);
consoleOutput.printf("Hey there! This is %S.\n", "Lubaina Khan");
consoleOutput.print("Today you're exploring the PrinWriter class with Code Gym. ");
consoleOutput.println("Hope you're having fun!");
consoleOutput.append("Patience is the key when learning new concepts.\n");
consoleOutput.append("It all boils down to practise and persistence. :)");
consoleOutput.flush();
consoleOutput.close();
}
}
์ฐ์ถ
์ด๋ด! ๋ฃจ๋ฐ์ด๋ ์นธ์
๋๋ค. ์ค๋์ Code Gym์ผ๋ก PrinWriter ํด๋์ค๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค. ์ฆ๊ฑฐ์ด ์๊ฐ ๋ณด๋ด์๊ธฐ ๋ฐ๋๋๋ค! ์๋ก์ด ๊ฐ๋
์ ๋ฐฐ์ธ ๋ ์ธ๋ด์ฌ์ด ํต์ฌ์
๋๋ค. ๋ชจ๋ ๊ฒ์ ์ฐ์ต๊ณผ ๋๊ธฐ๋ก ๊ท๊ฒฐ๋ฉ๋๋ค. :)
PrintWriter ํด๋์ค์ ์ฌ์ฉ๋ ๋ฉ์๋
printf(String str, Object arguments);
printf () ๋ฉ์๋๋ ์ธ์ํ ๋ฌธ์์ด์ ํ์์ ์ทจํฉ๋๋ค. ์ฌ๊ธฐ์ ์๋ฆฌ ํ์์ %S ๋ ๋ฌธ์์ด ์์ ์ ๋ฌ๋ ๋๋ฌธ์ ์ธ์๋ก ๋์ฒด๋ฉ๋๋ค.
print(String str);
์ด ๋ฉ์๋๋ PrintWriter ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ์ฌ ์ ๋ฌ๋ ๋ฌธ์์ด์ ์ธ์ํฉ๋๋ค .
println(String str);
๋ฌธ์์ด ๋ด์ฉ ๋ค์ ์ค ๋ฐ๊ฟ์ด ์ธ์๋ฉ๋๋ค.
append(CharSequence cs);
์ถ๊ฐ์ ์ ๋ฌ๋ ๋ฌธ์ ์ํ์ค๋ PrintWrtier ๊ฐ์ฒด์ ์ถ๊ฐ๋ฉ๋๋ค.
flush();
PrintWriter ๊ฐ์ฒด ์ ๋ด์ฉ์ ๋น์๋๋ค .
close();
์ฐ๊ธฐ ์คํธ๋ฆผ์ ๋ซ๊ณ ํ ๋น๋ ๋ฆฌ์์ค๋ฅผ ํด์ ํฉ๋๋ค.
์ 2
์ด ์์ ๋ ๋ฐ์ดํฐ๋ฅผ ํ์ผ์ ์ฐ๊ธฐ ์ํด PrintWriter ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๋ณด์ฌ์ค๋๋ค .
import java.io.PrintWriter;
import java.util.Date;
import java.util.Locale;
public class PrintWriterDemo {
public static void main(String[] args) throws Exception {
try {
// by importing the java.io.PrintWriter class
PrintWriter fileOutput = new PrintWriter("FileOutput.txt");
fileOutput.printf(Locale.getDefault(), "Hi, What's the day today? %s.\n", new Date());
fileOutput.print("Here's an implementation of PrinWriter class for file writing.\n");
fileOutput.println("Hope Code Gym made it simpler for you to understand.");
fileOutput.append("One step at a time, and off you go!", 0, 35);
fileOutput.flush();
fileOutput.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
์ฐ์ถ
์๋
ํ์ธ์, ์ค๋์ ๋ฌด์จ ์์ผ์
๋๊น? Sun Jul 25 17:30:21 PKT 2021. ๋ค์์ ํ์ผ ์ฐ๊ธฐ๋ฅผ ์ํ PrinWriter ํด๋์ค ๊ตฌํ์
๋๋ค. ํฌ๋ง์ฝ๋์ง์ ์ฌ๋ฌ๋ถ์ด ์ดํดํ๊ธฐ ์ฝ๊ฒ ๋ง๋ค์์ต๋๋ค. ํ ๋ฒ์ ํ ๊ฑธ์, ๊ทธ๋ฆฌ๊ณ ์ถ๋ฐ!
PrintWriter ํด๋์ค์ ์ฌ์ฉ๋ ๋ฉ์๋
์ฝ์์ ์ฐ๋ ๊ฒ๊ณผ ๊ตฌ๋ณ๋๋ ํ์ผ ์ฐ๊ธฐ์ ์ฌ์ฉ๋๋ ๋ฐฉ๋ฒ์ ๋ํด ๋ ผ์ํด ๋ด ์๋ค.
printf(Locale locale, String str, Object args);
์ฌ๊ธฐ์์ ํ์์ ๋ฐ๋ผ ์ฌ์ฉํ ์ ์๋ ๋ก์ผ์ผ(์ผ๊ด์ฑ์ ์ํด ์์คํ
๊ธฐ๋ณธ๊ฐ์ ์ฌ์ฉํจ)์ ์ ๋ฌํ ์ ์์ต๋๋ค. ์ด๊ฒ์ ๋ชจ๋ ์ง์ญ ๊ธฐ๋ฐ ํ์์ ์ค์ํฉ๋๋ค. ๋๋จธ์ง ๊ตฌํ์ ์ด์ ์ ์ฌ์ฉ๋ ๊ฒ๊ณผ ๋์ผํฉ๋๋ค.
append(CharSequence cs, int beginningIndex, int endingIndex);
์์ ๋ฐ ๋ ์ธ๋ฑ์ค๋ฅผ ์ง์ ํ์ฌ ์ ๋ฌ๋ CharSequence ์ฒญํฌ๋ฅผ ์ถ๊ฐํ ์ ์์ต๋๋ค . ์ฌ๊ธฐ์๋ ๋ง์ง๋ง ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํ์ต๋๋ค. ์ด๋ฅผ ๊ฐ์ง๊ณ ๋๋ฉด์ ๋ค๋ฅธ ์ถ๋ ฅ์ ๋ณผ ์ ์์ต๋๋ค.
try{
...
} catch (Exception e){
...
}
try-catch ๋ธ๋ก์ ํ์ผ ์ฐ๊ธฐ์ ๋ฐ๋์ ์ฌ์ฉ๋ฉ๋๋ค. ์์คํ
์์ ํ์ผ์ ์ ๊ทผํ๊ฑฐ๋(์: ๊ถํ ๋ฌธ์ ) ํ์ผ์ ์์ฑํ๋ ๋์ ์์ธ๊ฐ ๋ฐ์ํ์ง ์๋๋ก ํ๊ธฐ ์ํจ์
๋๋ค.
GO TO FULL VERSION