<1> <CDP: Socket Programming> Part 4 자바네트워크프로그래밍 1. Java IO 2. 스레드 (Thread) 클래스의소개 3. Java Socket
<2> <CDP: Socket Programming> 1. 자바입출력 Java_source->src->ch11
<3> <CDP: Socket Programming> 1.0 I/O Stream logic
<4> <CDP: Socket Programming> 1.0 서버와클라이언트의소켓준비
<5> <CDP: Socket Programming> 1.1 스트림개념 (1/3) 스트림 순서가있고, 길이가정해져있지않은데이터흐름 종류...InputStream 자바입출력 바이트 ( 바이너리, 숫자 ) 텍스트 ( 문자 )...OutputputStream...Reader...Writer
<6> <CDP: Socket Programming> 1.1 스트림개념 (2/3) 바이트스트림 바이너리 (binary) 데이터와숫자를읽고, 쓰기위해서사용. - 예 : 그림파일, 동영상파일, 응용프로그램파일등 바이트단위로입출력작업 InputStream Java Program OutputStream
<7> <CDP: Socket Programming> 1.1 스트림개념 (3/3) 문자스트림 문자스트림은문자데이터를읽고, 쓰기위해서사용. 인코딩을자동으로변환해줌 KSC5601 인코딩 Reader 유니코드인코딩 Java Program Writer KSC5601 인코딩
<8> <CDP: Socket Programming> 1.2 자바입출력클래스분류와상속관계 (1/8) 용도에따른분류 데이터싱크스트림 - 데이터를근원지에서직접읽거나, 목적지에기록하는클래스들 데이터프로세싱스트림 - 다른스트림을이용해서중간에서어떤작업을수행하는클래스들 샤워의비유 수도꼭지 -> 싱크스트림 샤워기, 수도꼭지와샤워기를연결하는것 -> 프로세싱스트림 싱크스트림 Java Program 프로세싱스트림
<9> <CDP: Socket Programming> 1.2 자바입출력클래스분류와상속관계 (2/8) 클래스들분류
<10> <CDP: Socket Programming> 1.2 자바입출력클래스분류와상속관계 (3/8) 싱크스트림클래스들 Console System.in/out
<11> <CDP: Socket Programming> 1.2 자바입출력클래스분류와상속관계 (4/8) 데이터프로세싱스트림클래스 데이터프로세싱스트림은항상다른스트림을이용해서만생성가능 데이터프로세싱스트림의생성자는다른스트림을매개변수로받게됨
<12> <CDP: Socket Programming> 1.2 자바입출력클래스분류와상속관계 (5/8) 바이트스트림상속관계 InputStream 과 OutputStream 으로부터상속받음. 회색은싱크스트림클래스
<13> <CDP: Socket Programming> 1.2 자바입출력클래스분류와상속관계 (6/8) 문자스트림상속관계 Reader 와 Writer 클래스로부터상속받는다. 회색은싱크스트림클래스
<14> <CDP: Socket Programming> 1.2 자바입출력클래스분류와상속관계 (7/8) Input Stream chain exam Data Source program FileInputStream BufferedInputStream DataInputStream
<15> <CDP: Socket Programming> 1.2 자바입출력클래스분류와상속관계 (8/8) OutputStream chain exam program Data Source DataOutputStream BufferedOutputStream FileOutputStream
<16> <CDP: Socket Programming> 1.2.1 InputStream/OutputStream 추상클래스 (1/8) InputStream 클래스 바이트 (byte) 입력스트림을위한가장상위클래스. System.in 대표적인메소드들 -int read() - 입력스트림에서한바이트를읽어서리턴. - int read(byte[] b, int off, int len) - 입력스트림에서 len 개의바이트를읽어서바이트배열 b 에저장. - int read(byte[] b) - 배열의크기만큼읽는다. 읽은바이트수리턴.
<17> <CDP: Socket Programming> 11.2.1 InputStream/OutputStream 추상클래스 (2/8) 예제 : SimpleRead.java 8 try { 9 b = System.in.read(); 10 while(b!= -1) { 11 System.out.print((char)b); 12 count++; 13 b = System.in.read(); 14 } l결과 C:\> java SimpleRead Hello Hello 한글??±? ^Z total bytes:13 15 } catch(ioexception e) { 16 System.out.println(e); 17 } 18 System.out.println(); 19 System.out.println("total byte:" + count);
<18> <CDP: Socket Programming> 1.2.1 InputStream/OutputStream 추상클래스 (3/8) 콘솔 Re-Direction C: > java SimpleRead < SimpleRead.java import java.io.ioexception;... C: > java SimpleRead < SimpleRead.java > output.txt C: > more output.txt import java.io.ioexception;
<19> <CDP: Socket Programming> 1.2.1 InputStream/OutputStream 추상클래스 (4/8) ConsoleReader 클래스 콘솔에서데이터를쉽게읽기위한사용자정의클래스 예제 : ConsoleReader.java 3 public class ConsoleReader extends BufferedReader { 4 public ConsoleReader(Reader in) throws IOException { 5 super(in); 6 } 7 8 public ConsoleReader(InputStream in) throws IOException { 9 super(new InputStreamReader(in)); 10 } 11 12 public int readint() throws IOException { 13 return Integer.parseInt(readLine().trim());
<20> <CDP: Socket Programming> 1.2.1 InputStream/OutputStream 추상클래스 (5/8) 예제 : ConsoleReader.java( 계속 ) 16 public String readstring() throws IOException { 17 return readline().trim(); 18 } 19 20 public char readchar() throws IOException { 21 String line = readline().trim(); 22 return line.charat(0); 23 } 24 25 public double readdouble() throws IOException { 26 Double d = new Double(readLine().trim()); 27 return d.doublevalue();
<21> <CDP: Socket Programming> 1.2.1 InputStream/OutputStream 추상클래스 (6/8) 예제 : ConsoleReaderTest.java 11 ConsoleReader in = new ConsoleReader(System.in); 12 System.out.print(" 정수를입력해주세요 :"); 13 ivalue = in.readint(); 14 System.out.println("value = " + ivalue); 15 System.out.print(" n실수를입력해주세요 :"); 16 dvalue = in.readdouble(); 17 System.out.println("value = " + dvalue); 18 System.out.print(" n문자를입력해주세요 :"); 19 cvalue = in.readchar(); 20 System.out.println("value = " + cvalue); 21 System.out.print(" n문자열을입력해주세요 :"); 22 svalue = in.readstring(); 23 System.out.println("value = " + svalue);
<22> <CDP: Socket Programming> 1.2.1 InputStream/OutputStream 추상클래스 (7/8) Copier 클래스 입력스트림에서읽은데이터를출력스트림으로출력하는사용자정의클래스 예제 : Copier.java 3 public class Copier { 4 protected InputStream in; 5 protected OutputStream out; 6 protected byte data[]; 7 8 public Copier(InputStream in, OutputStream out, int sz) { 9 this.in = in; 10 this.out = out; 11 data = new byte[sz];
<23> <CDP: Socket Programming> 11.2.1 InputStream/OutputStream 추상클래스 (8/8) 예제 : Copier.java( 계속 ) 14 public Copier(InputStream in, OutputStream out) { 15 this(in, out, 1024); 16 } 17 18 public void copy() throws IOException { 19 int n = 0; 20 while((n = in.read(data))!= -1) { 21 out.write(data, 0, n); 22 } 23 in.close(); 24 out.close();
<24> <CDP: Socket Programming> 1.2.2 FileInputStream/FileOutputStream (1/4) FileInputStream 파일에서바이트단위로내용을읽기위해사용 생성자 - FileInputStream(File file) - FileInputStream(String name) FileOutputStream 파일로바이트단위로내용을출력하기위해사용 생성자 - FileOutputStream(File file) - FileOutputStream(String name, boolean append) - FileOutputStream(String name)
<25> <CDP: Socket Programming> 1.2.2 FileInputStream/FileOutputStream (2/4) 예제 : ConsoleCopy.java 4 public static void main(string args[]) { 5 if(args.length!= 1) { 6 System.out.println( 7 "usage: java ConsoleCopy <filename>"); 8 System.exit(1); 9 } 10 try { 11 Copier cp = new Copier(System.in, 12 new FileOutputStream(args[0])); 13 cp.copy(); l 결과 C:\> java ConsoleCopy abc.txt 한글입니다. ^Z C:\> more abc.txt 한글입니다.
<26> <CDP: Socket Programming> 1.2.2 FileInputStream/FileOutputStream (3/4) 예제 : More.java 4 public static void main(string args[]) { 5 if(args.length!= 1) { 6 System.out.println( 7 "usage: java More <filename>"); 8 System.exit(1); 9 } 10 try { 11 Copier cp = new Copier(new FileInputStream(args[0]), 12 System.out); 13 cp.copy(); l 결과 C:\> java More More.java import java.io.*; public class More {...
<27> <CDP: Socket Programming> 1.2.2 FileInputStream/FileOutputStream (4/4) 예제 : FileCopy.java 5 if(args.length!= 2) { 6 System.out.print("usage: java FileCopy "); 7 System.out.println("<filename> <filename>"); 8 System.exit(1); 9 } 10 try { 11 Copier cp = new Copier(new FileInputStream(args[0]), 12 new FileOutputStream(args[1])); 13 cp.copy(); 14 System.out.println(" 파일복사가완료되었습니다."); l 결과 C:\> java FileCopy abc.txt bc.txt 파일복사가완료되었습니다. C:\> more bc.txt 한글입니다.
<28> <CDP: Socket Programming> 1.2.3 FileReader/FileWriter FileReader 파일에서문자단위로내용을읽을때사용 생성자 - FileReader(File file) FileWriter - FileReader(String filename) 파일에문자데이터를출력할때사용 생성자 - FileWriter(File file) - FileWriter(String filename, boolean append) - FileWriter(String filename)
<29> <CDP: Socket Programming> 1.2.3 FileReader/FileWriter 예제 : ReadFileData.java 10 try { 11 ConsoleReader in = new ConsoleReader( 12 new FileReader("data.txt")); 13 ivalue = in.readint(); 14 System.out.println("value = " + ivalue); 15 dvalue = in.readdouble(); 16 System.out.println("value = " + dvalue); 17 cvalue = in.readchar(); 18 System.out.println("value = " + cvalue); 19 svalue = in.readstring(); 20 System.out.println("value = " + svalue);
<30> <CDP: Socket Programming> 1.2.4 InputStreamReader/OutputStreamWriter InputStreamReader 입력스트림을 Reader 타입으로변환 생성자 - InputStreamReader(InputStream in, String enc) - InputStreamReader(InputStream in) OutputStreamWriter 출력스트림을 Writer 클래스로변환 생성자 - OutputStreamWriter(OutputStream out, String enc) - OutputStreamWriter(OutputStream out)
<31> <CDP: Socket Programming> 1.2.5 PrintStream/PrintWriter PrintStream 데이터출력 ( 예 : System.out, System.err) 생성자 - PrintStream(OutputStream out, boolean autoflush) - PrintStream(OutputStream out) PrintWriter 문자데이터출력 생성자 - PrintWriter(OutputStream out, boolean autoflush) - PrintWriter(OutputStream out) - PrintWriter(Writer out, boolean autoflush) - PrintWriter(Writer out)
<32> <CDP: Socket Programming> 1.2.5 PrintStream/PrintWriter ConsoleWriter 출력을보다편리하게할수있는사용자정의클래스 예제 : ConsoleWriter.java 4 public class ConsoleWriter extends PrintWriter { 5 public ConsoleWriter(OutputStream out) { 6 super(out, true); 9 public ConsoleWriter(Writer out) { 10 super(out, true); 13 public void printwon(long value) { 14 String format = "W#,###"; 15 DecimalFormat df = new DecimalFormat(format); 16 print(df.format(value));
<33> <CDP: Socket Programming> 11.2.5 PrintStream/PrintWriter 예제 : ConsoleWriter.java( 계속 ) 24 public void printdollar(float value) { 25 String format = "$#,###.00"; 26 DecimalFormat df = new DecimalFormat(format); 27 print(df.format(value)); 35 public void print(double value, int digit) { 36 String format = "#."; 37 for(int i=0; i < digit; i++) { 38 format += "#"; 39 } 40 DecimalFormat df = new DecimalFormat(format); 41 print(df.format(value)); 42 flush();
<34> <CDP: Socket Programming> 11.2.5 PrintStream/PrintWriter 예제 : ConsoleWriterTest.java 5 ConsoleWriter out = new ConsoleWriter(System.out); 6 double d = 123.456789; 7 int money = 1234567; 8 9 out.println(d); 10 out.println(d, 2); 11 out.printlnwon(money); 12 out.printlndollar(money); l 결과 C:\> java ConsoleWriterTest 123.456789 123.46 W1,234,567 $1,234,567.00
<35> <CDP: Socket Programming> 1.2.6 BufferedInputStream/BufferedOutputStream BufferedInputStream 바이트입력에서성능을높이기위해서버퍼를사용하는클래스 생성자 - BufferedInputStream(InputStream in, int size) - BufferedInputStream(InputStream in) BufferedOutputStream 바이트출력에서성능을높이기위해서버퍼를사용하는클래스 생성자 - BufferedOutputStream(OutputStream out, int size) - BufferedOutputStream(OutputStream out)
<36> <CDP: Socket Programming> 1.2.7 BufferedReader/BufferedWriter (1/3) BufferedReader 버퍼를사용하는 Reader 생성자및중요메소드 - BufferedReader(Reader in, int sz) - BufferedReader(Reader in) - String readline() BufferedWriter 버퍼를사용하는 Writer 생성자 - BufferedWriter(Writer out, int sz) - BufferedWriter(Writer out)
<37> <CDP: Socket Programming> 1.2.7 BufferedReader/BufferedWriter (2/3) 예제 : ConfigurationReader.java 28 BufferedReader br = 29 new BufferedReader(new FileReader(file) ); 30 31 while((rl=br.readline())!= null) { 32 rl = rl.trim(); 33 if(rl.charat(0) == comment ) { 34 continue; 35 } else { 36 StringTokenizer st = 37 new StringTokenizer(rl,delm,false); 38 String key = st.nexttoken(); 39 String value = st.nexttoken(); 40 ht.put(key, value);
<38> <CDP: Socket Programming> 1.2.7 BufferedReader/BufferedWriter (3/3) 예제 : ConfigurationReader.java( 계속 ) 46 public String getvalue(string name) { 47 return (String)ht.get(name); 48 } 49 50 public static void main(string args[]) { 51 ConfigurationReader cr = l 결과 C:\> java ConfigurationReader 7690 it.mokpo.ac.kr admin@it.mokpo.ac.kr 52 new ConfigurationReader("server.conf"); 53 try { 54 cr.parse(); 55 System.out.println(cr.getValue("port")); 56 System.out.println(cr.getValue("server")); 57 System.out.println(cr.getValue("admin"));
<39> <CDP: Socket Programming> 1.2.8 DataInputStream/DataOutputStream DataInputStream 데이터타입별로읽을때사용 생성자및중요메소드 - DataInputStream(InputStream in) char readchar() double readdouble() int readint() DataOutputStream 데이터타입별로출력 생성자및중요메소드 - DataOutputStream(OutputStream out) void writechar(int v) void writeint(int v)
<40> <CDP: Socket Programming> 1.2.8 DataInputStream/DataOutputStream 예제 : DataIOTest.java 12 public void write() throws IOException { 13 DataOutputStream out = new 14 DataOutputStream(new FileOutputStream(file)); 15 int value[] = Fibonachi.calculate(n); 16 for(int i=0; i <= n; i++) { 17 out.writeint(value[i]); 18 //System.out.println(value[i]); 19 } 20 out.flush(); 21 out.close();
<41> <CDP: Socket Programming> 1.2.8 DataInputStream/DataOutputStream 예제 : DataIOTest.java( 계속 ) 24 public void read() throws IOException { 25 DataInputStream in = 26 new DataInputStream(new FileInputStream(file)); 27 int n = in.available()/4; 28 //System.out.println("# of data = " + n); 29 for(int i=0; i < n; i++) { 30 int v = in.readint(); 31 System.out.println("f["+i+"] = " + v); 32 } 33 in.close(); 34 }