nme.kr

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

다음 판
이전 판
프로그램:java:ㄴhomework:book_source:ch18 [2022/01/04 11:32]
clayeryan@gmail.com 만듦
프로그램:java:ㄴhomework:book_source:ch18 [2023/07/13 17:29] (현재)
clayeryan@gmail.com ↷ 문서가 programmer:java:ㄴhomework:book_source:ch18에서 프로그램:java:ㄴhomework:book_source:ch18(으)로 이동되었습니다
줄 1: 줄 1:
-====== Chapter18 입출력 스트림 ======+====== 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>