텍스트 스트림은 2 Byte 문자 체계를 가진 텍스트의 입출력에 사용한다.


형식별 2 Byte 출력

Console

OutputStreamWriter osw = new OutputStreamWriter(System.out);
BufferedWriter bw = new BufferedWriter(osw);
PrintWriter pw = new PrintWriter(bw);
pw.println(....);


File

File f = new File("파일이름");
FileWriter fw = new FileWriter(f, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
pw.println(....);

Network

Socket soc = new Socket(.....);
OutputStreamWriter osw = new OutputStreamWriter(soc.getOutputStream());
BufferedWriter bw = new BufferedWriter(osw);
PrintWriter pw = new PrintWriter(bw);
pw.println(....);


예제)

import java.io.*;

public class Text_IO_Output_test {
	public static void main(String[] ar) {
		try {
			/* 콘솔 출력 
			OutputStreamWriter osw = new OutputStreamWriter(System.out);
			BufferedWriter bw = new BufferedWriter(osw);
			PrintWriter pw = new PrintWriter(bw);
			*/
			
			File f = new File("./text_io_test.txt");
			FileWriter fw = new FileWriter(f, true);
			BufferedWriter bw = new BufferedWriter(fw);
			PrintWriter pw = new PrintWriter(bw);

			pw.println("Text I/O Output Test!");
			pw.close();			
		} catch (Exception ex) {}
	} 
}


형식별 2 Byte 입력

Console

InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader br = new BufferedReader(isr);
br.readLine();


File

File f = new File("파일이름");
FileReader fr = new FileReader(f, true);
BufferedReader br = new BufferedReader(fr);
br.readLine();

Network

Socket soc = new Socket(.....);
InputStreamReader isr = new InputStreamReader(soc.getInputStream());
BufferedReader br = new BufferedReader(isr);
br.readLine();


예제)

import java.io.*;

public class Text_IO_Input_test {
	public static void main(String[] ar) {
		try {
			/* 콘솔 입력
			InputStreamReader isr = new InputStreamReader(System.in);
			BufferedReader br = new BufferedReader(isr);
			
			System.out.print("뭐든 써봐: ");
			String str = br.readLine();
			System.out.println("내용:");
			System.out.print(str);
			*/
			
                        // 파일 입력 
			File f = new File("./text_io_test.txt");
			FileReader fr = new FileReader(f);
			BufferedReader br = new BufferedReader(fr);
			
			System.out.println("읽은내용:");
			while(true) {
				String str = br.readLine();
				if (str == null) break;
				System.out.println(str);
			}
			
			br.close();
			
		} catch (Exception e) {
			
		}
	}
}

Posted by 후니아부지
: