(1) 스트림( Stream ): '데이터의 흐름'
1) Data Source(근원지) --------> Data Destination(목적지)
(2) 스트림 특징
1) FIFO ( First In First Out )
2) 지연성
3) 단방향성
4) 유연성
(3) 표준입력 / 표준출력
1) 표준입력: 키보드( System.in ) 사용법
<1> 근본스트림( Node Stream )
ex) InputStream is = System.in;
<2> 다리스트림( Bridge Stream )
ex) InputStreamReader r = new InputStreamReader(is);
<3> 목적(응용)스트림( Filter Stream )
ex) BufferedReader br = new BufferedReader(r);
2) 표준출력: 모니터(System.out) 사용법
<1> 근본스트림(Node Stream)
ex) PrintStream ps = System.out;
void input() {
try{
String line = br.readLine();
ps.println("키보드에서 읽은 값: " + line);
}catch(IOException ie){}
}
(4) 구분
1) 전송단위
<1> 바이트 스트림 ( 1 byte Stream )
ex) XXXStream
<2> 문자 스트림 ( 2 byte Stream )
ex) XXXReader, XXXWriter
2) 특성
<1> 근본 스트림 ( Node Stream )
ex) System.in, System.out, 등등...
<2> 다리 스트림 ( Bridge Stream )
ex) InputStreamReader, OutputStreamWriter
<3> 목적 스트림 ( Filter Stream )
ex) BufferedReader, PrintWriter, 등등...
3) 입출력
<1> 입력 스트림
ex) XXXInputStream, XXXReader
<2> 출력 스트림
ex) XXXOutputStream, XXXWriter
(5) 주요 스트림 클래스
1) InputStream / OutputStream 사용법
ex) InputStream is = System.in;
OutputStream os;
B() {
try{
os = new FileOutputStream("b.txt");
}catch(FileNotFoundException fe){}
}
void in1() {
try{
int i = 0;
while((i=is.read()) != -1){
os.write(i);
if(i==13){
break;
}
}
os.flush();
}catch(IOException ie){
}finally{
try{
is.close();
os.close();
}catch(IOException ie){}
}
}
void in2() { // in2() 메소드는 int read(byte bs[])를 이용한 방법이다.
byte bs[] = new byte[8]; //계란판
try{
int i = 0;
while((i=is.read(bs)) != -1){
os.write(bs, 0, i);
}
os.flush();
}catch(IOException ie){
}finally{
try{
is.close();
os.close();
}catch(IOException ie){}
}
}
2) FileInputStream / FileOutputStream 사용법
ex) String fname = "C:/SBC/Java/A.java"; // 복사하고싶은 파일 이름이다.
FileInputStream fis; //Node Stream : 파일을 읽기 위한 스트림이다.
OutputStream os = System.out; //Node Stream : 모니터에 쓰기 위한 스트림이다.
FileOutputStream fos; //Node Stream : 파일에 쓰기 위한 스트림이다.
String fcopy = "A.java"; // 복사해서 저장할 때의 이름이다.
C() {
try{
fis = new FileInputStream(fname);
fos = new FileOutputStream(fcopy);
}catch(FileNotFoundException fe){
System.out.println(fname+"이란 파일을 못찾음");
}
}
void r1() { // 모니터에 출력하는 메소드이다.
byte bs[] = new byte[512];
try{
int i = 0;
while((i=fis.read(bs)) != -1){
os.write(bs, 0, i);
}
os.flush();
}catch(IOException ie){
}finally{
try{
fis.close();
os.close();
}catch(IOException ie){}
}
}
void r2() { // 파일에 출력하는 메소드이다.
byte bs[] = new byte[1024];
try{
int i = 0;
long total = 0L;
while((i=fis.read(bs)) != -1){
fos.write(bs, 0, i);
total += i;
}
fos.flush();
System.out.println("복사("+fcopy+") 완료("+total+")bytes!!");
}catch(IOException ie){
}finally{
try{
fis.close();
fos.close();
}catch(IOException ie){}
}
}
3) BufferedInputStream / BufferedOutputStream 사용법
ex) String fname = ""C:/SBC/Java/A.java""; // 복사하고싶은 파일 이름이다.
FileInputStream fis; //Node Stream : 파일을 읽기 위한 스트림이다.
OutputStream os = System.out; //Node Stream : 모니터에 쓰기 위한 스트림이다.
FileOutputStream fos; //Node Stream : 파일에 쓰기 위한 스트림이다.
String fcopy = "A.java"; // 복사해서 저장할 때의 이름이다.
BufferedInputStream bis; // Filter Stream
BufferedOutputStream bos; // Filter Stream
D() {
try{
fis = new FileInputStream(fname);
bis = new BufferedInputStream(fis, 4096);
fos = new FileOutputStream(fcopy);
bos = new BufferedOutputStream(fos, 4096);
}catch(FileNotFoundException fe){
System.out.println(fname+"이란 파일을 못찾음");
}
}
void copy() {
byte bs[] = new byte[1024];
try{
int i = 0;
long total = 0L;
while((i=bis.read(bs)) != -1){
bos.write(bs, 0, i);
total += i;
}
bos.flush();
System.out.println("복사("+fcopy+") 완료("+total+")bytes!!");
}catch(IOException ie){
}finally{
try{
bis.close();
bos.close();
fis.close();
fos.close();
}catch(IOException ie){}
}
}
4) DataInputStream / DataOutputStream 사용법
ex) String fname = "A.java";
FileOutputStream fos; //Node
DataOutputStream dos; //Filter
FileInputStream fis; //Node;
DataInputStream dis; //Filter
E() {
try{
fos = new FileOutputStream(fname);
fis = new FileInputStream(fname);
}catch(FileNotFoundException fnfe){}
dos = new DataOutputStream(fos);
dis = new DataInputStream(fis);
}
void w() { // Source -> File
byte b = 10; //1
short s = 20; //2
char c = 'a'; //2
int i = 40; //4
long lo = 50L; //8
float f = 60.0f; //4
double d = 70.0; //8
boolean flag = false; //1
String str = "호랑이";//
try{
dos.writeByte(b);
dos.writeShort(s);
dos.writeChar(c);
dos.writeInt(i);
dos.writeLong(lo);
dos.writeFloat(f);
dos.writeDouble(d);
dos.writeBoolean(flag);
dos.writeUTF(str);
dos.flush();
}catch(IOException ie){
}finally{
try{
dos.close();
fos.close();
}catch(IOException ie){}
}
}
void r() { // File -> Monitor
try{
byte b = dis.readByte();
short s = dis.readShort();
char c = dis.readChar();
int i = dis.readInt();
long lo = dis.readLong();
float f = dis.readFloat();
double d = dis.readDouble();
boolean flag = dis.readBoolean();
String str = dis.readUTF();
System.out.println("b: " + b + ", s: " + s + ", c: " + c +", i: " + i);
System.out.println("lo: " + lo + ", f: " + f + ", d: " + d +", flag: " + flag);
System.out.println("str: "+ str);
}catch(IOException ie){}
}
5) Reader / Writer / FileReader / FileWriter 사용법
ex) String fName = "A.java";
OutputStream os = System.out; //1byte
Reader r;
Writer w;
FileWriter fw;
A() {
try{
r = new FileReader(fName);
w = new OutputStreamWriter(os);
fw = new FileWriter("A.txt");
}catch(FileNotFoundException fe){
System.out.println(fName+"이란 파일을 못찾음");
}catch(IOException ie){}
}
void rw() {
int i = 0;
char ch[] = new char[8];
try{
while((i=r.read(ch)) != -1){
w.write(ch, 0, i);
fw.write(ch, 0, i);
}
w.flush();
fw.flush();
}catch(IOException ie){
}finally{
try{
fw.close();
w.close();
os.close();
r.close();
}catch(IOException ie){}
}
}
7) BufferedReader / PrintWriter 사용법
ex) InputStream is = System.in; //Node
InputStreamReader isr = new InputStreamReader(is); //Bridge
BufferedReader br = new BufferedReader(isr); //Filter
FileWriter fw; //Node
PrintWriter pw; //Filter : autoFlush, many method, bridge X
B() {
try{
fw = new FileWriter("B.txt");
pw = new PrintWriter(fw, true);
}catch(IOException ie){}
}
void rw() {
String line = "";
try{
while((line = br.readLine()) != null){
pw.println(line);
}
}catch(IOException ie){
}finally{
try{
if(pw != null) pw.close();
if(fw != null) fw.close();
if(br != null) br.close();
if(isr != null) isr.close();
if(is != null) is.close();
}catch(IOException ie){}
}
}
(6) 응용
1) 파일 복사:
https://github.com/SHIN96BC/KOSMO-2021-2022-JAVA/tree/main/11_%ED%8C%8C%EC%9D%BC%EB%B3%B5%EC%82%AC
2) 폴더 복사:
'Java > Java' 카테고리의 다른 글
Java 16. GUI( Graphic User Interface ) (0) | 2022.03.11 |
---|---|
Java 15. NetWork (0) | 2022.03.11 |
Java 13. 람다식 (0) | 2022.03.11 |
Java 12. 쓰레드 ( Thread ) (0) | 2022.03.11 |
Java 11. 어노테이션 ( Annotation ) (0) | 2022.03.11 |