Java Programming Language (4)

Download Report

Transcript Java Programming Language (4)

Java Programming Language (4)
Company
Logo
@
Dong In Shin
DCS lab
Seoul National University
입출력 기능에 따른 stream의 분류(filtering)

목적 : 기존의 스트림에 원하는 입출력 기능을 제공하
는 스트림을 연결하여 새로운 스트림을 만든다.
[email protected]
입출력 기능에 따른 stream의 분류






자료를 문자열 표현으로 출력
Buffering : 효율적인 입출력
다중연결(Concatenating) : 여러 inputstream
을 하나의 stream으로 만든다.
되돌림(Pushbacking): 일부 입력의 취소
자료를 내부 표현 그대로 입출력
객체의 내용을 입출력
[email protected]
PrintWriter , PrintStream

자료를 문자열 표현으로 출력
new PrintWriter(Writer out, boolean autoflush=false);
new PrintWriter(OutputStream out, boolean autoflush=false);
print(Type data);
println(Type data);


Type : int, long, float, double, boolean, char,
char[], String, Object
PrintStream 역시 PrintWriter와 거의 동일하게 동작하나,
byte stream class이므로 논리적으로는 부적절하다.  가능
하면 PrintWriter를 사용
[email protected]
PrintWriter Example
[email protected]
Buffering Stream


내부 buffer를 제공함으로써 입출력의 효율성을 높임
BufferedReader, BufferedWriter의 경우에는 해당 운영
체제에 적합한 행단위의 입출력 기능도 제공( readLine )
String readLine();

한 행의 문자열을 읽어서 ‘행 분리자’ 문자열을 버리고 나머지 문
자열을 반환한다.
newLine();

해당 운영체제의 ‘디폴트 행 분리자’ 문자열( line.separator )
을 출력
[email protected]
Buffering Example
[email protected]
[email protected]
되돌림 스트림(Pushbacking)

이미 입력된 byte나 character를 stream으로 되
돌릴 수 있는 stream
[email protected]
[email protected]
Input stream 을 일련의 token으로 변환


Input stream을 parsing을 통하여 토큰(token)으로 변환
토큰(token)

단어, 숫자, 스트링, 보통 문자(+,-), 줄 주석(//)
[email protected]
[email protected]
[email protected]
기본 자료형 자료의 내부 표현 입출력

기본 자료형의 자료를 내부 표현 그대로 입출력

1 byte 이상을 차지하는 자료형의 자료는 상위 바이트부터 입출력
(big endian order 혹은 network byte order)
[email protected]
DataOutput/DataInput Interface

기본 자료형 자료의 내부 표현 입출력을 위한 method 정의
write( int b );
writeByte( int b );
write( byte b[] );
write( byte b[], int off, int len );
writeShort( int v );
writeInt( int b );
writeLong( long b );
writeFloat( float b );
writeDouble( double b );
writeBoolean( boolean b );
writeChar( int b );
writeChars( String b );
[email protected]
byte readByte();
int readUnsignedByte();
readFully(byte buf[]);
readFully(byte buf[], int off,
int len);
int skipBytes(int n);
Short readShort();
int readUnsignedShort();
int readInt();
long readLong();
double readDouble();
boolean readBoolean();
char readChar();
[email protected]
[email protected]
객체 직렬화(object serialization)



객체의 내용을 파일 혹은 네트워크를 통하여 byte stream으로
입출력
http://java.sun.com/products/jdk/1.1/docs/guide/ser
ializaton
http://wwwipd.ira.uka.de/~hauma/betterserial/
[email protected]
ObjectOutput/ObjectInput interface

기본 자료형의 자료 및 객체를 입출력하기 위한 method 정의
writeObject(Object obj);
flush();
close();
Object readObject() throws ClassNotFoundException
int read();
int read(byte buf[]);
int read(byte buf[], int off, int len);
[email protected]
[email protected]
[email protected]
Serializable interface


Serializable interface를 구현하지 않는 class의 객체는 객체직렬
화로 입출력 할 수 없다.  NotSerializableException 발생
많은 표준 class들은 Serializable interface를 구현


Transient로 선언되지 않은 instance 변수가 입출력
instance 변수가 객체를 참조하는 경우, 그 객체도 입출력

( 그 객체의 class 역시 Serializable interface를 구현해야 한다 )
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
Serializable interface

Serializable을 구현하는 class에 다음 method가 정의되면, 그
class에 직접 정의되어 있는 인스턴스 변수가 입출력되려고 할 때 , 대
신 다음 method가 호출 된다.
Private void writeObject(ObjectOutputStream stream)
Private void readObject(ObjectInputStream stream)
[email protected]
[email protected]
[email protected]
[email protected]