본문 바로가기

2023.11.21-2024.05.31

240103 JAVA IO(OutputStream,InputStream)

 

바이트 출력 스트림 : OutputStream

 바이트 기반 출력 스트림의 최상위 클래스로 추상 클래스이다. 모든 바이트 기반 출력 스트림 클래스는 OutputStream 클래스를 상속받아서 만드러진다.

 -FileOutpitStream

 -PrintStream

 -BufferedOutStream

 -DateOutputStream

 

바이트 입력 스트림 : InputStream
 q바이트 기반 입력 스트림의 최상위 클래스로 추상 클래스이다. 모든 바이트 기반 입력 스트림은 InputStream 클래스를 상속받아서 만들어진다.

 -FileInputStream

 -BufferedInputStream

 -DataInputStream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.sukgi.io;
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
 
//input output = io
//stdio 592p
public class IO01 {
    public static void main(String[] args) throws IOException {
        
        OutputStream os = new FileOutputStream("c:/temp/test.txt");
        byte a = 65;
        byte b = 67;
        byte c = 68;
        
        os.write(a);
        os.write(b);
        os.write(c);
        
        
        os.flush();
        os.close();
                
    }
}
 
cs

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.sukgi.io;
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
 
//input output = io
//stdio 592p
public class IO02 {
    public static void main(String[] args) throws IOException {
        
        OutputStream os = new FileOutputStream("c:/temp/test2.txt");
        
        byte arr[] = new byte[] {66,69,72,72,79};
        //1byte = bit
        
        os.write(arr, 13);
        
        os.flush();
        os.close();
                
    }
}
 
cs

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.sukgi.io;
 
import java.io.FileInputStream;
import java.io.InputStream;
 
public class IO03 {
 
    public static void main(String[] args) throws Exception{
        
        InputStream is = new FileInputStream("c:/temp/test3.txt");
        
        while (true) {
            int data = is.read();
            if(data == 11) {
                break;
            }
        System.out.println((char)data);
        
        }
 
    }
 
}
 
cs

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.sukgi.io;
 
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Arrays;
 
public class IO04 {
 
    public static void main(String[] args) throws Exception {
 
        InputStream is = new FileInputStream("c:/temp/test.txt");
        byte arr[] = new byte[2];
        while (true) {
            int readByteNum = is.read(arr);
            if (readByteNum == -1) {
                break;
            }
 
            for (int i = 0; i < readByteNum; i++) {
                System.out.print((char)arr[i]);
            }
        }
        is.close();
 
    }
 
}
 
cs

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.sukgi.io;
 
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
 
public class IO05 {
    public static void main(String[] args) throws Exception {
        FileOutputStream fos = new FileOutputStream("c:/temp/test4.txt");// 얘가 fos
        Writer writer = new OutputStreamWriter(fos);
        
        writer. write("문자열을 저장합니다.");
        writer.flush();
        writer.close();
    
    }
    
    
}
 
cs

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.sukgi.io;
 
import java.io.File;
import java.sql.Date;
import java.text.SimpleDateFormat;
 
//640P
//파일 클래스를 이용한 파일 및 폴더 정보 출력
public class IO06 {
    public static void main(String[] args) {
        File dir = new File("c:/temp/images");
        
        if(dir.exists()== false) {
            if(dir.exists()== false){
                dir.mkdirs();
                
                }
        File temp = new File("c:/temp");
        File contents[] = temp.listFiles();
        
        System.out.println("시간,\t\t\t형태\t\t크기\t이름");
    
        System.out.println("==============================================");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd a HH:mm");
        for (File file : contents) {
            System.out.println(sdf.format(new Date(file.lastModified())));
            if(file.isDirectory()) {
                System.out.println("\t<DIR\t\t\t" + file.getName());
                
            }else {
                System.out.println("\t\t\t"+ file.length()+ "\t"+ file.getName());
            }
            System.out.println("");
        }
        
        
        
        }
        
    }
    
}
 
cs

'2023.11.21-2024.05.31' 카테고리의 다른 글

240102 HTML delete.jsp  (1) 2024.01.03
240102 HTML index.jsp  (0) 2024.01.03
240103 JAVA Stack  (0) 2024.01.03
240103 JAVA Inner  (0) 2024.01.03
240103 JAVA Queue01  (0) 2024.01.03