[JAVA] Day 18 - Stream

sueยท2023๋…„ 12์›” 10์ผ

๐Ÿ“’๊ตญ๋น„ํ•™์› [JAVA]

๋ชฉ๋ก ๋ณด๊ธฐ
16/20
post-thumbnail

Stream :

  • ๋‹จ๋ฐฉํ–ฅ (์Œ์œผ๋กœ ๋งŒ๋“ค์–ด์ ธ์žˆ์Œ)
  • ์ถœ๋ ฅ : ๋ฐ์ดํ„ฐ๋ฅผ๋ฐ”๊นฅ์œผ๋กœ ๋‚ด๋ณด๋‚ผ๋•Œ -> OutStream
  • ์ž…๋ ฅ : ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„๋“ค์ผ ๋•Œ -> InStream

๐Ÿ”Ž [Eclipse] - [package] - [class] ์ƒ์„ฑ


๐Ÿ“Œ Note Code


  • System.in.read( ) = int ๊ฐ’์œผ๋กœ ๋ฐ์ดํ„ฐ ์ฝ์–ด์˜ด



1. Stream์˜ ๊ฐœ๋… / ๊ธฐ๋ณธ ์ž…์ถœ๋ ฅ์ŠคํŠธ๋ฆผ

Test1

๐Ÿ’ป ์ž…๋ ฅ

package com.day18;

import java.io.IOException;

//in (์ž…๋ ฅ)
public class Test1 {

	public static void main(String[] args) throws IOException {

		int data;
		System.out.print("๋ฌธ์ž์—ด ์ž…๋ ฅ");
		while((data=System.in.read())!=-1) { //๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค๋ฉด
			char ch = (char)data; 
			System.out.print(ch);
		}
	}
}


๐Ÿ’ก **์ถœ๋ ฅ**
๋ฌธ์ž์—ด ์ž…๋ ฅabc123
abc123



๐Ÿ“Œ Note Code


  • System.in : ํ‚ค๋ณด๋“œ์— ์ž…๋ ฅํ•˜๋Š” ๋‚ด์šฉ
  • System.out : ์ปดํ“จํ„ฐ์•ˆ์— ์žˆ๋Š” ๋‚ด์šฉ์„ ์ฝ˜์†”์ฐฝ์— ๋„์›Œ์ฃผ๋Š” ๊ฒƒ
  • OutputStream os = System.out; // OutputStream os(ๅค–) : ๊ฐ์‹ธ๊ณ  ์žˆ์Œ <- System.out;(ๅ…ง)
  • buffer : ๋ฐ์ดํ„ฐ๋ฅผ ์ผ์‹œ์ ์œผ๋กœ ์ €์žฅํ•˜๋Š” ์ž„์‹œ ๋ฉ”๋ชจ๋ฆฌ (์ถœ๋ ฅํ•  ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๊ณ ์žˆ์Œ)
    ์‚ฌ์šฉ์ด์œ ? ์ž…์ถœ๋ ฅ ์†๋„์ฐจ๋กœ ์ธํ•œ ํšจ์œจ์„ฑ โ†‘



2. buffer๊ฐœ๋… / ๊ธฐ๋ณธ ์ž…์ถœ๋ ฅ์‹œ์Šคํ…œ

Test2

๐Ÿ’ป ์ž…๋ ฅ

import java.io.OutputStream;

//out : ์ถœ๋ ฅ์šฉ
public class Test2 {

	public static void main(String[] args) {

		try {
			OutputStream os = System.out; //๊ธฐ๋ณธ์ŠคํŠธ๋ฆผ์„ ์•„์›ƒํ’‹์ŠคํŠธ๋ฆผ์œผ๋กœ ๊ฐ์‹ธ์คŒ
			
			byte[] buffer = new byte[3]; //buffer์— ๋‹ด์•„์„œ ํ•œ๋ฒˆ์— ๋‚ด๋ณด๋ƒ„
		
			buffer [0] = 65; 
			buffer [1] = 97; 
			buffer [2] = 122; 
			
			//65์ด์ง€๋งŒ, 2byte๋ผ์„œ 1byte์”ฉ ๊ฐ€์ ธ์™€์•ผ ํ•จ
	
			os.write(buffer); //buffer์— ๋‹ด์€ ๋‚ด์šฉ์„ ์ถœ๋ ฅ์ŠคํŠธ๋ฆผ์— ์”€
			//os.close(); //๋ฐ˜๋“œ์‹œ close
			
			System.out.println();
			System.out.println("์ถœ๋ ฅ๊ฐ’ ๋ณด์—ฌ?");
			
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}


๐Ÿ’ก **์ถœ๋ ฅ**
Aaz
์ถœ๋ ฅ๊ฐ’ ๋ณด์—ฌ?



๐Ÿ“Œ Note Code


  • InputStreamReader :
    โ‘  ๋ฐ์ดํ„ฐ ์ €์žฅ x -> ๊ทธ๋ƒฅ ํ†ต๊ณผํ•˜๋Š” ๋‹ค๋ฆฌ (BridgeStream
    )
    โ‘ก ์‚ฌ์šฉ์ž๊ฐ€ ํ‚ค๋ณด๋“œ๋กœ ์ž…๋ ฅํ•œ(system.in์ด ์ž…๋ ฅํ•œ ๊ฐ’ - ) 1byte ๋ฌธ์ž ํ•˜๋‚˜๋ฅผ 2byte๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” Stream
    โ‘ข ํŒŒ์ผ / ๋„คํŠธ์›Œํฌ ์†Œ์ผ“์—์„œ ์ฝ์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ ์‚ฌ์šฉ

  • Reader(๋ณด๊ธ‰ํ˜•) : ์ €์žฅ๋Šฅ๋ ฅ O- BufferedReader(๊ณ ๊ธ‰ํ˜•) : ๋ชจ์•„๋‘” ๋ฐ์ดํ„ฐ๋“ค์„ ๊ฐ€์žฅ ํšจ์œจ์ ์œผ๋กœ ์ €์žฅํ–ˆ๋‹ค๊ฐ€ ์‰ฝ๊ฒŒ ์ž…์ถœ๋ ฅํ•˜๊ฒŒ๋” ๋งŒ๋“  ๋ฐ์ดํ„ฐ ์ €์žฅ๊ณต๊ฐ„

  • rd.read( ) : 1byte์”ฉ ์ฝ์–ด์˜ด



3. InputStreamReader / OutputStreamReader / Reader

Test3

๐Ÿ’ป ์ž…๋ ฅ


import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

//InputStreamReader : Bridge
//Output StreamReader

//buffer 
//bufferedReader : ์ €์žฅ๊ณต๊ฐ„

public class Test3 {

	public static void main(String[] args) throws IOException {

		
	int data;
	System.out.print("๋ฌธ์ž์—ด?");
	
	Reader rd = new InputStreamReader(System.in);

	while((data=rd.read())!=-1){ 
		char ch = (char)data;
		
		System.out.print(ch);
		}
	}
}


๐Ÿ’ก **์ถœ๋ ฅ**
๋ฌธ์ž์—ด?ab12๊ฐ€๋‚˜
ab12๊ฐ€๋‚˜



๐Ÿ“Œ Note Code


  • OutputStreamReader
    โ‘  OutputStream์—์„œ ์ฝ์€ 1byte ๋ฌธ์ž๋ฅผ ๋ฌธ์ž ์ธ์ฝ”๋”ฉ(ex, UTF-8)์— ๋”ฐ๋ผ 2byte ์œ ๋‹ˆ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜ํ•ด์„œ ์“ฐ์—ฌ์ง
    โ‘ก Writer๋กœ ๋ณ€ํ™˜
    โ‘ก ํŒŒ์ผ / ๋„คํŠธ์›Œํฌ ์†Œ์ผ“ ๋“ฑ ๋ฌธ์ž์—ด์„ ์“ธ ๋•Œ

-writer์€ ๋ฐ˜๋“œ์‹œ flush๋ฅผ ์จ์•ผ ๊ฐ•์ œ๋กœ ๋ฐ์ดํ„ฐ ๋‚ด๋ณด๋‚ด์ง



4. OutputStreamReader / Reader / Writer

Test4

๐Ÿ’ป ์ž…๋ ฅ


import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;

public class Test4 {

	public static void main(String[] args) throws IOException {

		int data;
		System.out.print("๋ฌธ์ž์—ด?");
		
		Reader rd = new InputStreamReader(System.in);//ํ‚ค๋ณด๋“œ ์ž…๋ ฅ๋œ ๊ฐ’์„ ํ†ต๊ณผํ•˜๋Š” ๋‹ค๋ฆฌ๋ฅผ ํ†ตํ•ด ์ €์žฅ์†Œrd์— ์ €์žฅํ•ด๋ผ
		Writer wr = new OutputStreamWriter(System.out); //์ฝ˜์†”์— ๋‚ด๋ณด๋ƒ„
		
		while((data=rd.read())!=-1) {
			wr.write(data); 
			wr.flush(); //writer -flush ์ง๊ฟ
		}
	}
}


๐Ÿ’ก **์ถœ๋ ฅ**
๋ฌธ์ž์—ด?ab123๊ฐ€๋‚˜
ab123๊ฐ€๋‚˜



๐Ÿ“Œ Note Code


  • FileinputStream [class] :
    โ‘  ํŒŒ์ผ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์˜ค๊ธฐ
    โ‘ก read( ) ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ํŒŒ์ผ์—์„œ byte๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ์Œ
    โ‘ข ์˜ˆ์™ธ์ฒ˜๋ฆฌ : try-catch๋ฌธ ํ•„์ˆ˜



5. FileInputStream / ํŒŒ์ผ์— ์žˆ๋Š” ๋‚ด์šฉ์„ ์ฝ์–ด์˜ค๋Š”๋ฒ•

Test5

๐Ÿ’ป ์ž…๋ ฅ


import java.io.FileInputStream;

public class Test5 {

	public static void main(String[] args) {

		try {
			
			FileInputStream fis = new FileInputStream("c:\\doc\\test.txt");
			
			int data;
			while((data=fis.read())!=-1){
				
				//System.out.print((char)data);
				System.out.write(data); //์ฝ˜์†”๋กœ ์ž…๋ ฅ
				System.out.flush(); //๋‚จ์€ ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
				
			}
		} catch (Exception e) {
		
		}
	}
}


๐Ÿ’ก **์ถœ๋ ฅ**
//System.out.print((char)data);
์ด๋ ‡๊ฒŒ ์ถœ๋ ฅํ•˜๋ฉด ์™ธ๊ณ„์–ด ์†ก์ถœ
?ยฟ?ร˜ยนยฐยฐ? ยน??????? ยธยถยธ?ยฐ? ยด???ยท?
??ยด?ยด??? ยบยธยฟ????? ยฟ?ยธยฎยณยชยถ? ยธยธยผยผ
ยน?ยฑ??ยญ ????ยธยฎ ?ยญยท?ยฐยญ?? 
------------------------------------------
๋™ํ•ด๋ฌผ๊ณผ ๋ฐฑ๋‘์‚ฐ์ด ๋งˆ๋ฅด๊ณ  ๋‹ณ๋„๋ก
ํ•˜๋А๋‹˜์ด ๋ณด์šฐํ•˜์‚ฌ ์šฐ๋ฆฌ๋‚˜๋ผ ๋งŒ์„ธ
๋ฌด๊ถํ™” ์‚ผ์ฒœ๋ฆฌ ํ™”๋ ค๊ฐ•์‚ฐ
------------------------------------------
//๋งŒ์ผ System.out.flush(); ๋‚จ์€ ๋ฐ์ดํ„ฐ๋ฅผ ์•ˆ๋ถˆ๋Ÿฌ๋“ค์ด๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ์ถœ๋ ฅ๋จ
๋™ํ•ด๋ฌผ๊ณผ ๋ฐฑ๋‘์‚ฐ์ด ๋งˆ๋ฅด๊ณ  ๋‹ณ๋„๋ก
ํ•˜๋А๋‹˜์ด ๋ณด์šฐํ•˜์‚ฌ ์šฐ๋ฆฌ๋‚˜๋ผ ๋งŒ์„ธ




๐Ÿ“Œ Note Code


  • FileOutputStream - ๋ณด๊ธ‰ํ˜•
    โ‘  ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋‚ด์šฉ์„ ํŒŒ์ผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ -> ์œ„์น˜ ์šฐ๋ฆฌ๊ฐ€ ์ €์žฅํ•ด์ฃผ๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅ
    โ‘ก file์„ ๋งŒ๋“ค ์ˆœ ์žˆ์ง€๋งŒ, ์‚ญ์ œ๋Šฅ๋ ฅ X
    โ‘ข while๋ฌธ์„ ์ผ๋‹ค๋ฉด ๋ฐ˜๋“œ์‹œ close๋กœ ๋‹ซ์•„์ค˜์•ผํ•จ
    โ‘ฃ write( )๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ byte๋ฅผ ํŒŒ์ผ์— ์”€
    โ‘ค ์˜ˆ์™ธ์ฒ˜๋ฆฌ : try-catch๋ฌธ ํ•„์ˆ˜



6. FileOutputStream

Test6

๐Ÿ’ป ์ž…๋ ฅ


import java.io.FileOutputStream;

//FileOutputStream 
public class Test6 {

	public static void main(String[] args) {

		try {
			
			FileOutputStream fos = new FileOutputStream("c:\\doc\\out1.txt");
			
			int data;
			System.out.print("๋ฌธ์ž์—ด ์ž…๋ ฅ");
			
			//๋ช‡๊ธ€์ž๋‚˜ ์ž…๋ ฅํ• ์ง€ ๋ชจ๋ฅด๋‹ˆ while + ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’์„ ์ฝ์–ด๋‚ผ๊ฑฐ๋‹ˆ๊นŒ System.in.read()
			while((data=System.in.read())!=-1) {
				
				fos.write(data);

			}
			fos.close(); // while๋ฌธ ์‚ฌ์šฉ์‹œ, ๋ฐ˜๋“œ์‹œ ๋‹ซ์•„์ค˜์•ผํ•จ
			
		} catch (Exception e) {
			System.out.println(e.toString());
		}
	}
}


๐Ÿ’ก **์ถœ๋ ฅ**
๋ฌธ์ž์—ด ์ž…๋ ฅab123 -> ๋‚ด๊ฐ€ ์ง€์ •ํ•œ ์œ„์น˜์— ํ•ด๋‹นํŒŒ์ผ ์ด๋ฆ„ + ๋‚ด์šฉ ์ €์žฅ๋จ



๐Ÿ“Œ Note Code


โ‘  Scanner ๋จผ์ € ๊ฐ’ ๋ฐ›๊ธฐ
โ‘ก ํŒŒ์ผ์— ์žˆ๋Š” ๋‚ด์šฉ ์ฝ์–ด์˜ค๊ธฐ (FileInputStream) / ๋‚ด์šฉ ํŒŒ์ผ ์ €์žฅํ•˜๊ธฐ (FileInputStream)



7. โญ FileInputStream / FileOutputStream ํŒŒ์ผ ๋ณต์‚ฌ ํ”„๋กœ๊ทธ๋žจ

Test7

๐Ÿ’ป ์ž…๋ ฅ


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;

public class Test7 {

	public static void main(String[] args) {

	try {
	Scanner sc = new Scanner(System.in);
		

		String str1,str2;
		int data;
		
		System.out.print("์›๋ณธํŒŒ์ผ?");
		str1 = sc.next();
		
		System.out.print("๋Œ€์ƒํŒŒ์ผ?");
		str2 = sc.next();

		//์™ธ์›Œ์•ผํ•จ!!!!!!!
		FileInputStream fis = new FileInputStream(str1);
		FileOutputStream fos = new FileOutputStream(str2);
		
		while((data=fis.read())!=-1){
			
			fos.write(data); 
			fos.flush(); 
			
		}
		
		fis.close();
		fos.close();

		System.out.println("๋ณต์‚ฌ์™„๋ฃŒ");

	} catch (Exception e) {

	}
}
}


๐Ÿ’ก **์ถœ๋ ฅ**
์›๋ณธํŒŒ์ผ?c:\\doc\\test.txt
๋Œ€์ƒํŒŒ์ผ?c:\\doc\\out2.txt = ๋ณต์‚ฌ
๋ณต์‚ฌ์™„๋ฃŒ



๐Ÿ“Œ Note Code


  • ํ•ด๋‹นํŒŒ์ผ์—์„œ ์ฝ์–ด์˜จ ๋‚ด์šฉ์„ FileInputStream [fis]์— ๋‹ด๊ณ  -> ๊ทธ๊ฑธ BufferedReader (new InputStreamReader(fis))๋ฅผ ํ†ตํ•ด 2byte๋กœ ์ฝ์–ด๋ƒ„
  • br.readLine() ์œ„์— ๋ฉ”์ธ๋ฉ”์„œ๋“œ์— ์—๋Ÿฌ์ฒ˜๋ฆฌ ์•ˆํ•˜๋Š” ์ด์œ ? try-catch๋ฌธ์— Exception์˜ˆ์™ธ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ



8. ํŒŒ์ผ์—์„œ ์ฝ์–ด์˜จ ๋‚ด์šฉ(FileInputStream)์„ bufferedReader๋กœ ๋ถ€๋ฅด๊ธฐ

Test8

๐Ÿ’ป ์ž…๋ ฅ


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class Test8 {

	public static void main(String[] args) {
		try {

			FileInputStream fis = new FileInputStream("c:\\doc\\test.txt");
			// ํ•ด๋‹นํŒŒ์ผ์—์„œ ์ฝ์–ด์˜จ ๋‚ด์šฉ์„ fis์— ๋„ฃ๊ณ  -> ๊ทธ๊ฑธ ์•„๋ž˜ inputstreamReader(fis)์„ ํ†ตํ•ด 2byte๋กœ ์ฝ์–ด๋ƒ„

			BufferedReader br = new BufferedReader(new InputStreamReader(fis));

			String str;
			while ((str = br.readLine()) != null) { // string ๋ฐ˜ํ™˜๊ฐ’ null
				System.out.println(str);
			}

			fis.close();

		} catch (Exception e) {
			System.out.println(e.toString());
		}
	}
}


๐Ÿ’ก **์ถœ๋ ฅ**
ั‹... //์•Œ์ˆ˜์—†๋Š” ๊ธ€์ž ์†ก์ถœ : ๋ฐ์ดํ„ฐ์–ธ์–ด๊ธฐ๋•Œ๋ฌธ์— 



๐Ÿ“Œ Note Code


  • PrintStream [class] - ๊ณ ๊ธ‰ํ˜•
    โ‘  ๋‚ด๋ณด๋‚ด๋Š” ์žฅ์น˜ [์ถœ๋ ฅ์ŠคํŠธ๋ฆผ]
    โ‘ก ๋ฌธ์ž์—ด์„ ๊ฐ„ํŽธํ•˜๊ฒŒ ์ถœ๋ ฅํ•˜๋Š”๋ฐ ์‚ฌ์šฉ
    โ‘ข ์ž์ฒด๊ฐ€ ํŒŒ์ผ์— ์ง์ ‘ ๋ฐ์ดํ„ฐ๋ฅผ ์“ธ ์ˆœ ์—†๊ณ , ๋‹ค๋ฅธ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์„ ๋ž˜ํ•‘(๊ฐ์‹ธ๋Š”๊ฒƒ)ํ•ด์„œ ์‚ฌ์šฉํ•จ (PrintStream์ด FileOutputStream์„ ๊ฐ์Œˆ)
    โ‘ฃ println, printf, print๊ณผ ๊ฐ™์€ ์ถœ๋ ฅ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ
PrintStream ps = new PrintStream(new FileOutputStream("example.txt"));
ps.println("Hello, World!");
ps.close();



9. ํƒ€์ดํ•‘์น˜๋Š” ๋‚ด์šฉ์„ ํŒŒ์ผ์— ์ €์žฅํ•˜๊ธฐ

Test9

๐Ÿ’ป ์ž…๋ ฅ


import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;

public class Test9 {

	public static void main(String[] args) {

		try {
			FileOutputStream fos = new FileOutputStream("c:\\doc\\out4.txt");

			PrintStream ps = new PrintStream(fos); 

			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));// ํ‚ค๋ณด๋“œ์—์„œ ์ €์žฅ๋œ ๋‚ด์šฉ์„ br์—๋‹ค๊ฐ€ ์ €์žฅ

			String str;

			System.out.print("๋ฌธ์ž์—ด ์ž…๋ ฅ?");

			while ((str = br.readLine()) != null) // br์•ˆ์— ์žˆ๋Š” str์„ ์ฝ์–ด๋‚ด๋ผ
				ps.println(str); //line 17 / ps๊ฐ€ ๊ฐ€์ง€๊ณ ์žˆ๋Š”๊ฑธ๋กœ ๋‚ด๋ณด๋‚ด๋ผ => ๊ทผ๋ฐ ps๋Š” fos๋‚ด์šฉ์„ ๋‹ด๊ณ ์žˆ์Œ

			ps.close();
			fos.close();

		} catch (Exception e) {
			System.out.println(e.toString());
		}
	}
}


๐Ÿ’ก **์ถœ๋ ฅ**
๋ฌธ์ž์—ด ์ž…๋ ฅab123 -> ๋‚ด๊ฐ€ ์ง€์ •ํ•œ ์œ„์น˜์— ํ•ด๋‹นํŒŒ์ผ ์ด๋ฆ„ + ๋‚ด์šฉ ์ €์žฅ๋จ



๐Ÿ“Œ Note Code


  • File ํด๋ž˜์Šค
    โ‘  ํŒŒ์ผ์ •๋ณด, ๊ฒฝ๋กœ, ๋””๋ ‰ํ† ๋ฆฌ ์—ฌ๋ถ€, ์‚ญ์ œ ๋“ฑ ๊ฐ€๋Šฅ
    โ‘ก ํŒŒ์ผ ๋ฐ ํด๋”๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ํด๋ž˜์Šค
    โ‘ข ํŒŒ์ผ์„ ๋ณต์‚ฌ ๋ณ€๊ฒฝ ์กฐ์ž‘์„ ํ•  ๊ฒฝ์šฐ์—๋งŒ ์‚ฌ์šฉ๋˜๊ณ , ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ž…์ถœ๋ ฅ ํ•˜๊ธฐ์œ„ํ•œ ๋ฉ”์„œ๋“œ๋Š” ์—†์Œ



10. โญ File ํด๋ž˜์Šค

Test10

๐Ÿ’ป ์ž…๋ ฅ

import java.io.File;
import java.io.IOException;
import java.util.Date;

//File ํด๋ž˜์Šค
public class Test10 {

	public static void main(String[] args) throws IOException {

		File f = new File("c:\\doc\\test.txt"); //์ด๋ฏธ ์กด์žฌํ•˜๋Š” ํŒŒ์ผ์„ ๋‹ด์•„๋†“๋Š”๊ฒƒ
		
		if(!f.exists()) { //ํŒŒ์ผ์กด์žฌ์—ฌ๋ถ€ํ™•์ธ  
			//false
			System.out.println("ํŒŒ์ผ์ด ์—†๋‹ค");
			return;
		}//true
		
		System.out.println("ํŒŒ์ผ๋ช… : "+f.getName());
		System.out.println("ํŒŒ์ผ์‚ฌ์ด์ฆˆ : "+f.length());
		
		//2byte * ๊ธ€์ž์ˆ˜ + ์—”ํ„ฐ ์ˆ˜ (2byte *๊ฐœ์ˆ˜ - ๋งˆ์ง€๋ง‰์€ ์—”ํ„ฐ์•ˆํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ–ˆ์„ ๋•Œ) + ๋„์–ด์“ฐ๊ธฐ = ํŒŒ์ผ์‚ฌ์ด์ฆˆ
		
		//read (์‹คํ–‰๊ฐ€๋Šฅ? ๋ถˆ๊ฐ€๋Šฅ?)๋ผ๋Š” ๊ธฐ๋Šฅ์ด ์—†์œผ๋ฉด ๋”๋ธ”ํด๋ฆญํ•ด๋„ ์ฝ์„ ์ˆ˜ ์—†์Œ
		
		System.out.println("ํŒŒ์ผ๊ฒฝ๋กœ : "+f.getAbsolutePath()); 
		System.out.println("ํŒŒ์ผํ‘œ์ค€๊ฒฝ๋กœ : "+f.getCanonicalPath());  //์•ž์— ๋Œ€๋ฌธ์ž, ์˜ˆ์™ธ์ฒ˜๋ฆฌ
		
		//new Data๋Š” ๋‚ ์งœ๋งŒ ๊ฐ€์ง€๊ณ ์˜ฌ ์ˆ˜ ์žˆ์Œ (๋งˆ์ง€๋ง‰ ๋ณ€๊ฒฝ์ผ)
		//Calendar๋Š” ํ˜„์žฌ์‹œ๊ฐ„์„ ๊ฐ€์ง€๊ณ ์™€์„œ ๋‚ ์งœ๋ฅผ ๋ณ€ํ˜•์‹œํ‚ฌ ์ˆ˜ ์žˆ์Œ
		System.out.println("๋งŒ๋“  ๋‚  : "+new Date(f.lastModified())); 
		
		System.out.println("ํŒŒ์ผ๊ฒฝ๋กœ2 : "+f.getParent()); 
		System.out.println("์ฝ๊ธฐ์†์„ฑ : "+f.canRead());
		System.out.println("์“ฐ๊ธฐ์†์„ฑ : "+f.canWrite());
		
	}

}


๐Ÿ’ก **์ถœ๋ ฅ**
ํŒŒ์ผ๋ช… : test.txt
ํŒŒ์ผ์‚ฌ์ด์ฆˆ : 175
ํŒŒ์ผ๊ฒฝ๋กœ : c:\doc\test.txt
ํŒŒ์ผํ‘œ์ค€๊ฒฝ๋กœ : C:\doc\test.txt
๋งŒ๋“  ๋‚  : Fri Dec 08 01:42:55 KST 2023
ํŒŒ์ผ๊ฒฝ๋กœ2 : c:\doc
์ฝ๊ธฐ์†์„ฑ : true
์“ฐ๊ธฐ์†์„ฑ : true



๐Ÿ“Œ Note Code


  • if(!f.getParentFile().exists()) : ๋ถ€๋ชจ์˜ ๋””๋ ‰ํ† ๋ฆฌํŒŒ์ผ์ด ์—†๋‹ค๋ฉด -> f.getParentFile().mkdirs();๋กœ ์ผ๋‹จ ๋ถ€๋ชจ๋””๋ ‰ํ„ฐ๋ฆฌ(์ค‘๊ฐ„๋””๋ ‰ํ„ฐ๋ฆฌ๋„ ๊ฐ€๋Šฅ) ์ƒ์„ฑ
    ex) ํŒŒ์ผ ๊ฒฝ๋กœ๊ฐ€ "/path/java/score.txt"์ผ ๋•Œ, "/path/java/" ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด => ์ด๊ฒƒ์ด "๋ถ€๋ชจ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ƒ์„ฑ"



11. โญ File ํด๋ž˜์Šค / ๋‚ด๊ฐ€ ์“ฐ๋Š” ๋ฌธ์ž์—ด๋กœ ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ

Test11

๐Ÿ’ป ์ž…๋ ฅ

import java.io.File;
import java.io.FileOutputStream;


public class Test11 {

	public static void main(String[] args) {

		//index
		String str = "c:\\doc\\temp\\java\\test1.txt";
		
		
		/*
		๋ฐฉ๋ฒ•1
		
		 *str.substring() : ๋ฌธ์ž์—ด ์ถ”์ถœ
		๋’ค์—์„œ๋ถ€ํ„ฐ \\์ด๊ฑฐ ์ฐพ์Œ (์ธ๋ฑ์Šค๋ฒˆํ˜ธ-1)
		String path = str.substring(0, str.lastIndexOf("\\")); //c~java๊นŒ์ง€
		
		System.out.println(path);
		
		File f = new File(path); //outputStream์ด ๋งŒ๋“ค์–ด์•ผํ•จ
		
		if(!f.exists()) {
			f.mkdirs(); // java๊นŒ์ง€์˜ ๊ฒฝ๋กœ๋ฅผ ๋งŒ๋“ฆ (path=f๋ผ์„œ)
		}
		*/
		
		
        //๋ฐฉ๋ฒ•2 - ์ด ๋ฐฉ๋ฒ•์„ ์ž˜ ์”€
		File f = new File(str);
		if(!f.getParentFile().exists()) { /
			f.getParentFile().mkdirs();
		}
		

		
		
		//๊ณต์šฉ
		
		try {
			
			FileOutputStream fos = new FileOutputStream(str);
			
			System.out.println("๋ฌธ์ž์—ด ์ž…๋ ฅ?");
			
			int data;
			
			while((data = System.in.read())!=-1) {
				fos.write(data);
				fos.flush();
			}
			
			
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}


๐Ÿ’ก **์ถœ๋ ฅ**
๋ฌธ์ž์—ด ์ž…๋ ฅab123 -> ๋‚ด๊ฐ€ ์ง€์ •ํ•œ ์œ„์น˜์— ํ•ด๋‹นํŒŒ์ผ ์ด๋ฆ„ + ๋‚ด์šฉ ์ €์žฅ๋จ



๐Ÿ“Œ Note Code


  • FileFilter [Interface] :
    โ‘  implements FileFilter ํ•˜๋Š”๊ฒŒ ํฌ์ธํŠธ
    โ‘  ์˜ค๋ฒ„๋ผ์ด๋“œ (accept) -> true/false ๋ฐ˜ํ™˜
    โ‘ก ๋””์Šคํฌ ๋‚ด ๋ชจ๋“  ํด๋”์™€ ํŒŒ์ผ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณด์—ฌ์คŒ

  • listRoots() :
    ๊ฐ ๋“œ๋ผ์ด๋ธŒ(C:, D:) ๋“ฑ์„ ๋œปํ•จ



12. FileFilter

Test12

๐Ÿ’ป ์ž…๋ ฅ


import java.io.File;
import java.io.FileFilter;


//FileFilter
class MyFileList implements FileFilter {

	

	private File f; 
	
	//spring[framework์—์„œ ๋งŽ์ด ์‚ฌ์šฉ] :์˜์กด์„ฑ ์ฃผ์ž… - ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์™ธ๋ถ€์—์„œ ํ•„์š”ํ•œ ๊ฐ์ฒด ์ƒ์„ฑ 
	public MyFileList(String filePath) { //์ƒ์„ฑ์ž ์˜ค๋ฒ„๋กœ๋”ฉ ํ†ตํ•ด์„œ ๊ฐ์ฒด ์ƒ์„ฑ => ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ’ 1๊ฐœ 
    	
        //MyFileList๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ• ๋•Œ, ์•„๋ž˜ private๋„ ๊ฐ์ฒด์ƒ์„ฑํ•ด์ค˜์•ผํ•˜๋Š” ์ด์œ ? ํด๋ž˜์Šค ๋‚ด ํŒŒ์ผ f ์‚ฌ์šฉํ•˜๊ธฐ์œ„ํ•ด์„œ
		f = new File(filePath);
        
	}
	
	public void result() {
		
		try {
			
		
			if(!f.exists()) {
				System.out.println("ํŒŒ์ผ์ด ์—†์Šต๋‹ˆ๋‹ค!");
				return;
			}
			System.out.println("์ ˆ๋Œ€๊ฒฝ๋กœ : "+ f.getAbsolutePath());
			System.out.println("ํŒŒ์ผํฌ๊ธฐ : "+ f.length());

			if(f.isDirectory()) {
				File[] lists = f.listFiles(this); //์ž๊ธฐ์ž์‹ ์˜ file๋“ค์˜ list๋ฅผ ๋„ฃ์–ด๋ผ => ๋ฐฐ์—ด์— ํŒŒ์ผ์ด ๋“ค์–ด๊ฐ
			//this = ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฒฝ๋กœ
				
				
			System.out.println("\nํด๋” ๋‚ด์šฉ..."); // = ํŒŒ์ผ
			
			for(int i =0; i<lists.length;i++) {
				
				System.out.print(lists[i].getName());
				System.out.println("\t"+lists[i].length());
			
			}
			}
			
		System.out.println("๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ์กฐ");//์‹ค์ œํด๋”๋“ค์ด ๋ณด์—ฌ์ง€๋Š” ๊ฒƒ 
		File[] root = File.listRoots();
		for(int i=0;i<root.length;i++) {
			System.out.println(root[i].getPath());
		}

		} catch (Exception e) {
			
		}
	}
	
	
	//์‹คํ–‰ํ•  ํ•„์š” ์—†์Œ , result๋ฉ”์„œ๋“œ๋งŒ ํ˜ธ์ถœํ•˜๋ฉด ๋จ 
	@Override
	public boolean accept(File pathname) {
		return pathname.isFile() || pathname.isDirectory(); //ํŒŒ์ผ๊ตฌ์กฐ + ํด๋”๊ตฌ์กฐ ๋ณด๊ณ ์‹ถ์„๋•Œ 
		
		//File[] lists = f.listFiles(this);
		//๊ฒฝ๋กœ๋งŒ ์ง€์ •ํ•ด์ฃผ๋ฉด = ๊ฒฝ๋กœ์— ์žˆ๋Š” ๋‚ด์šฉ์„ (override๋กœ) ์•ˆ์—์žˆ๋Š” ๊ตฌ์กฐ๋ฅผ ๋‹ค ์ฝ์–ด๋‚ด๊ณ  41์—๋‹ค๊ฐ€ ๋„ฃ์–ด์คŒ
		// => reuslt ํ˜ธ์ถœ
		
		//return pathname.isFile(); //ํŒŒ์ผ๊ตฌ์กฐ๋งŒ ๋ณด๊ณ ์‹ถ์„๋•Œ 
	}
	
}

public class Test12 {

	public static void main(String[] args) {

		MyFileList mf = new MyFileList("c:\\doc");
		mf.result();
		
	}

}


๐Ÿ’ก **์ถœ๋ ฅ**
์ ˆ๋Œ€๊ฒฝ๋กœ : c:\doc
ํŒŒ์ผํฌ๊ธฐ : 4096

ํด๋” ๋‚ด์šฉ...
out1.txt	0
out2.txt	175
out4.txt	0
score.txt	424
score1.txt	156
score2.txt	285
score3.txt	305
temp	0
test.txt	175
๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ์กฐ
C:\


0๊ฐœ์˜ ๋Œ“๊ธ€