nme.kr
Nav
Search
Edit
검색
가사
도서관
문학
사전
어학
역사
영화
클래식
프로그램
회화
html
keyboard
links
wiki
sidebar
전체 접힌 글 펴기 / 접기
문서 도구
문서 보기
이전 판
역링크
Fold/unfold all
문서 이름 바꾸기
사이트 도구
최근 바뀜
미디어 관리자
사이트맵
사용자 도구
관리
로그인
왼쪽 메뉴 편집
Plugin 관리
테마 디자인
카카오톡 링크 보내기
맨 위로
추적:
현재 위치:
index
»
프로그램
»
java
»
ㄴhomework
»
book_source
»
ch18
이 문서는 읽기 전용입니다. 원본을 볼 수는 있지만 바꿀 수는 없습니다. 문제가 있다고 생각하면 관리자에게 문의하세요.
====== Ch.18 입출력 스트림 ====== =====스트림 소개===== =====바이트 기반 입력 스트림===== <code java> package chapter18; import java.io.IOException; import java.io.InputStream; public class InputStreamEx { public static void main(String[] args) { try { InputStream in = System.in; int data = 0; while((data=in.read()) != -1) { System.out.print((char)data); } } catch (IOException e) { } // 한글 테스트 } } </code> <code java> package chapter18; import java.io.FileInputStream; import java.io.IOException; public class FieInputStreamEx { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("src/chapter17/InputStreamEx.java"); int data = 0; while((data=fis.read()) != -1) { System.out.print((char)data); } } catch (IOException e) { } } } </code> =====문자 기반 입력 스트림===== <code java> package chapter18; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; public class InputStreamEx2 { public static void main(String[] args) { try { // InputStreamReader 객체생성 Reader in = new InputStreamReader(System.in); int data = 0; while((data=in.read()) != -1) { System.out.print((char)data); } } catch (IOException e) { } } } </code> <code java> package chapter18; import java.io.FileInputStream; import java.io.IOException; public class FieInputStreamEx3 { public static void main(String[] args) { FileInputStream fis = null; try { fis = new FileInputStream("src/chapter17/InputStreamEx.java"); int data = 0; byte[] buf = new byte[fis.available()]; while((data=fis.read(buf, 0, buf.length)) != -1) { System.out.print(new String(buf, 0, data)); } } catch (IOException e) { } finally { try { fis.close(); } catch (Exception e) { System.out.println(e); } } } } </code> <code java> package chapter18; import java.io.FileInputStream; import java.io.IOException; public class FieInputStreamEx2 { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("src/chapter17/InputStreamEx.java"); int data = 0; byte[] buf = new byte[fis.available()]; while((data=fis.read(buf)) != -1) { System.out.print(new String(buf, 0, data)); } } catch (IOException e) { } } } </code> <code java> package chapter18; import java.io.FileReader; import java.io.IOException; public class FileReaderEx { public static void main(String[] args) { FileReader fr = null; try { fr = new FileReader("src/chapter17/InputStreamEx.java"); int data = 0; while ((data = fr.read()) != -1) { System.out.print((char)data); } } catch (IOException e) { } finally { try { fr.close(); } catch (Exception e) { System.out.println(e); } } } } </code> <code java> package chapter18; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class BufferedReaderEx { public static void main(String[] args) { FileReader fr = null; BufferedReader br = null; try { fr = new FileReader("src/chapter17/InputStreamEx.java"); br = new BufferedReader(fr); String txt = null; while ((txt = br.readLine()) != null) { System.out.println(txt); } } catch (IOException e) { } finally { try { fr.close(); br.close(); } catch (Exception e) { System.out.println(e); } } } } </code> =====바이트 기반 출력 스트림===== <code java> package chapter18; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class BufferedReaderEx { public static void main(String[] args) { FileReader fr = null; BufferedReader br = null; try { fr = new FileReader("src/chapter17/InputStreamEx.java"); br = new BufferedReader(fr); String txt = null; while ((txt = br.readLine()) != null) { System.out.println(txt); } } catch (IOException e) { } finally { try { fr.close(); br.close(); } catch (Exception e) { System.out.println(e); } } } } </code> <code java> package chapter18; import java.io.FileOutputStream; import java.io.IOException; public class FieOutputStreamEx2 { public static void main(String[] args) { try { FileOutputStream fos = new FileOutputStream("test2.txt", true); byte[] b = new byte[26]; byte data = 65; for (int i=0; i<b.length; i++) { b[i] = data; data++; } fos.write(b); } catch (IOException e) { System.out.println(e.getMessage()); } } } </code> <code java> package chapter18; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; public class PrintStreamEx { public static void main(String[] args) { try { FileOutputStream fos = new FileOutputStream("print.txt", true); PrintStream ps = new PrintStream(fos); ps.println("홍길동"); ps.println(1111); ps.println(true); ps.println(3.14); ps.flush(); ps.close(); } catch (FileNotFoundException e) { System.out.println(e.getMessage()); } } } </code> =====문자 기반 출력 스트림===== <code java> package chapter18; import java.io.FileWriter; import java.io.IOException; public class FileWriterEx { public static void main(String[] args) { try { FileWriter fw = new FileWriter("test3.txt"); // 문자하나 출력 fw.write('A'); char[] buf = {'B','C','D'}; // 문자배열 출력 fw.write(buf); // 문자배열 off부터 len개수만큼 출력 fw.write(buf,1,2); // 문자열 출력 fw.write("저는 홍길동입니다."); fw.flush(); fw.close(); } catch (IOException e) { System.out.println(e.getMessage()); } } } </code> <code java> package chapter18; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class BufferedWriterEx { public static void main(String[] args) { try { FileWriter fw = new FileWriter("test4.txt"); BufferedWriter bw = new BufferedWriter(fw); // 문자하나 출력 bw.write('A'); char[] buf = {'B','C','D'}; // 문자배열 출력 bw.write(buf); // 문자배열 off부터 len개수만큼 출력 bw.write(buf,1,2); // 문자열 출력 bw.write("저는 홍길동입니다."); bw.flush(); bw.close(); } catch (IOException e) { System.out.println(e.getMessage()); } } } </code>