자바 입출력(I/O) - 텍스트(Text) 입출력
앎/web 2014. 2. 24. 16:32 |텍스트 스트림은 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) { } } }
'앎 > web' 카테고리의 다른 글
[Servlet] MVC 패턴 (0) | 2014.02.26 |
---|---|
자바 입출력(I/O) - 객체(Obejct) 입출력 (0) | 2014.02.24 |
자바 입출력(I/O) - 스트림(Stream) 입출력 (0) | 2014.02.24 |
[Java Script] 사용자 정의 객체 & 내장 객체 (0) | 2014.02.18 |
[Java Script] Event & Handler (0) | 2014.02.18 |