문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 다음 판 | 이전 판 | ||
|
프로그램:java:ㄴhomework:book_source:ch18 [2022/01/04 11:32] clayeryan@gmail.com 만듦 |
프로그램:java:ㄴhomework:book_source:ch18 [2025/06/27 16:07] (현재) |
||
|---|---|---|---|
| 줄 1: | 줄 1: | ||
| - | ====== | + | ====== |
| + | =====스트림 소개===== | ||
| + | |||
| + | =====바이트 기반 입력 스트림===== | ||
| + | <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 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(" | ||
| + | int data = 0; | ||
| + | while((data=fis.read()) != -1) { | ||
| + | System.out.print((char)data); | ||
| + | } | ||
| + | } catch (IOException e) { | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | </ | ||
| + | =====문자 기반 입력 스트림===== | ||
| + | <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 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(" | ||
| + | int data = 0; | ||
| + | byte[] buf = new byte[fis.available()]; | ||
| + | while((data=fis.read(buf, | ||
| + | System.out.print(new String(buf, 0, data)); | ||
| + | } | ||
| + | } catch (IOException e) { | ||
| + | |||
| + | } finally { | ||
| + | try { | ||
| + | fis.close(); | ||
| + | } catch (Exception e) { | ||
| + | System.out.println(e); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | <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(" | ||
| + | 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 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(" | ||
| + | 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 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(" | ||
| + | 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 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(" | ||
| + | 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 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(" | ||
| + | |||
| + | byte[] b = new byte[26]; | ||
| + | byte data = 65; | ||
| + | for (int i=0; i< | ||
| + | b[i] = data; | ||
| + | data++; | ||
| + | } | ||
| + | fos.write(b); | ||
| + | } catch (IOException e) { | ||
| + | System.out.println(e.getMessage()); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | <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(" | ||
| + | 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 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(" | ||
| + | // 문자하나 출력 | ||
| + | fw.write(' | ||
| + | char[] buf = {' | ||
| + | // 문자배열 출력 | ||
| + | fw.write(buf); | ||
| + | // 문자배열 off부터 len개수만큼 출력 | ||
| + | fw.write(buf, | ||
| + | // 문자열 출력 | ||
| + | fw.write(" | ||
| + | |||
| + | fw.flush(); | ||
| + | fw.close(); | ||
| + | |||
| + | } catch (IOException e) { | ||
| + | System.out.println(e.getMessage()); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | <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(" | ||
| + | BufferedWriter bw = new BufferedWriter(fw); | ||
| + | |||
| + | // 문자하나 출력 | ||
| + | bw.write(' | ||
| + | char[] buf = {' | ||
| + | // 문자배열 출력 | ||
| + | bw.write(buf); | ||
| + | // 문자배열 off부터 len개수만큼 출력 | ||
| + | bw.write(buf, | ||
| + | // 문자열 출력 | ||
| + | bw.write(" | ||
| + | |||
| + | bw.flush(); | ||
| + | bw.close(); | ||
| + | |||
| + | } catch (IOException e) { | ||
| + | System.out.println(e.getMessage()); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | </ | ||
별도로 명시하지 않을 경우, 이 위키의 내용은 다음 라이선스에 따라 사용할 수 있습니다: CC Attribution-Share Alike 4.0 International