-Stream: ๋ฐ์ดํธ
-reader, writer: ํ ์คํธ
FileReader ์ฌ์ฉ
import java.io.FileReader;
import java.io.IOException;
public class FileReaderEx {
public static void main(String[] args) {
FileReader fin = null;
try {
fin = new FileReader("ascii");
int c;
while((c = fin.read()) != -1)
System.out.print((char) c);
fin.close();
}catch(IOException e) {e.getMessage();}
}
}
ascii ํ์ผ ๋ด์ฉ
์ถ๋ ฅ๊ฐ
ํ์ผ ๋ด์ฉ์ ํ๊ธ์ ์ ์ด๋ ์ ์ถ๋ ฅ๋๋ค.
FileInputStream ์ฌ์ฉ
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamEx {
public static void main(String[] args) {
FileInputStream fin = null;
try{
fin = new FileInputStream("ascii");
int c;
while((c = fin.read()) != -1)
System.out.print((char) c);
fin.close();
}catch(IOException e) {e.getMessage();}
}
}
๊ธฐ๋ณธ ๊ตฌ์ฑ๊ณผ ์ถ๋ ฅ๊ฐ์ ๋์ผํ๋, ํ๊ธ์ด๋ ํน์๋ฌธ์ ๋ฑ ์ ๋๋ก ์ถ๋ ฅ๋์ง ์์.
์์ ์ฝ๋์์ InputStreamReader๋ฅผ ์ฌ์ฉํด ํ๊ธ์ ์ฝ์ด๋ค์ผ ์ ์๋๋ก ๋ฐ๊ฟ์ค.
์ฝ๋ํ ์์คํ ์ด ๋ง์์ผ ์ ๋๋ก ์ฝ์ ์ ์๋ค๋ ์ ์ฃผ์!
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class FileInputStreamEx {
public static void main(String[] args) {
FileInputStream fin = null;
InputStreamReader in = null;
try{
fin = new FileInputStream("uni");
in = new InputStreamReader(fin, "MS949");
System.out.println(in.getEncoding());
int c;
while((c = in.read()) != -1)
System.out.print((char) c);
fin.close();
in.close();
}catch(IOException e) {e.getMessage();}
}
}
FileWriter ์ฌ์ฉ
writer๋ ์๋ ํ์ผ ๋ถ๋ฌ์๋ ์ฐ๊ธฐ ๋๋ฌธ์ ์๊ด์์!!
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class FileWriterEx {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
FileWriter fout = null;
char []buf = {'I', ' ', 'l', 'o','v','e',' ','j','a','v','a'};
try{
fout = new FileWriter("test.out");
while(true) {
String str = sc.nextLine();
if(str.length() == 0) //์๋ฌด๊ฒ๋ ์๋ ์ค์ด ๋์ค๋ฉด ๋๋จ
break;
fout.write(str); //์ง์ ์
๋ ฅ
fout.write("\n"); //๊ฐํ
}
//fout.write(buf); ํ๋ฒ์ buf ์ ์ฒด ์ถ๋ ฅํ๋ ค๋ฉด
fout.close();
}catch(IOException e) {e.getMessage();}
}
}
FileOutputStream ์ฌ์ฉ
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamEx {
public static void main(String[] args) {
FileOutputStream fout = null;
byte[] buf = {65,66,67,68,69,-1};
try{
fout = new FileOutputStream("binary");
for(int i = 0; i<buf.length; i++)
fout.write(buf[i]);
fout.close();
}catch(IOException e) {e.getMessage();}
}
}
binary ํ์ผ ๊ฒฐ๊ณผ
์์คํค์ฝ๋์ ํด๋นํ๋ ๋ฌธ์๋ ์ ๋จ์ง๋ง ๋ค๋ฅธ ๊ฑด ๋ญ ์๋ํ๋์ง ๊ณ์ฐํ๊ณ ๋ฃ๊ธฐ๊ฐ ์ด๋ ค์.
Visual C์์๋ ํฅ์ฌ์ฝ๋๋ก ๋ณด์ธ๋ค๊ณ ํจ.
๋ฒํผ ์ ์ถ๋ ฅ ์คํธ๋ฆผ BufferedOutputStream
import java.io.BufferedOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class BufferedOutputStreamEx {
public static void main(String[] args) {
FileReader fin = null;
BufferedOutputStream out = null;
try {
fin = new FileReader("digit");
out = new BufferedOutputStream(System.out, 5);
int c;
while((c = fin.read()) != -1)
out.write(c);
new Scanner(System.in).nextLine();
out.flush();
fin.close();
out.close();
}catch(IOException e) {e.getMessage();}
}
}