GiantStepDEV
article thumbnail

์ž…์ถœ๋ ฅ(I/O)?

  • Input๊ณผ Output์˜ ์•ฝ์ž
  • ์ปดํ“จํ„ฐ ๋‚ด๋ถ€ ๋˜๋Š” ์™ธ๋ถ€ ์žฅ์น˜์™€ ํ”„๋กœ๊ทธ๋žจ ๊ฐ„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›๋Š” ๊ฒƒ
  • ์žฅ์น˜์™€ ์ž…์ถœ๋ ฅ์„ ์œ„ํ•ด์„œ๋Š” ํ•˜๋“œ์›จ์–ด ์žฅ์น˜์— ์ง์ ‘ ์ ‘๊ทผ์ด ํ•„์š”ํ•œ๋ฐ ๋‹ค์–‘ํ•œ ๋งค์ฒด์— ์กด์žฌํ•˜๋Š” ๋ฐ์ดํ„ฐ๋“ค์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ž…์ถœ๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•  ๊ณตํ†ต์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ ์ŠคํŠธ๋ฆผ์„ ์ด์šฉํ•จ.

์ŠคํŠธ๋ฆผ

  • ์ž๋ฐ”์—์„œ๋Š” ํŒŒ์ผ์ด๋‚˜ ์ฝ˜์†”์˜ ์ž…์ถœ๋ ฅ์„ ์ง์ ‘ ๋‹ค๋ฃจ์ง€ ์•Š๊ณ , ์ŠคํŠธ๋ฆผ์ด๋ผ๋Š” ํ๋ฆ„์„ ํ†ตํ•ด ์ œ์–ด
  • ์‹ค์ œ์˜ ์ž…๋ ฅ์ด๋‚˜ ์ถœ๋ ฅ์ด ํ‘œํ˜„๋œ ๋ฐ์ดํ„ฐ์˜ ์ด์ƒํ™”๋œ ํ๋ฆ„
  • ์ฆ‰, ์ŠคํŠธ๋ฆผ์€ ์šด์˜์ฒด์ œ์— ์˜ํ•ด ์ƒ์„ฑ๋˜๋Š” ๊ฐ€์ƒ์˜ ์—ฐ๊ฒฐ ๊ณ ๋ฆฌ๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, ์ค‘๊ฐ„ ๋งค๊ฐœ์ž ์—ญํ• ์„ ํ•จ

์ŠคํŠธ๋ฆผ ํŠน์ง•

  • FIFO๊ตฌ์กฐ
    ๐Ÿถ ๋จผ์ € ๋“ค์–ด๊ฐ„ ๋ฐ์ดํ„ฐ๊ฐ€ ๋จผ์ € ๋‚˜์˜ค๋Š” ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ์˜ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ์ง€ ์•Š๋Š”๋‹ค
  • ๋‹จ๋ฐฉํ–ฅ
    ๐Ÿถ ํ•˜๋‚˜์˜ ์ŠคํŠธ๋ฆผ์—์„œ ์ž…์ถœ๋ ฅ์ด ๋™์‹œ์— ์ด๋ฃจ์–ด ์ง€์ง€ ์•Š๋Š”๋‹ค ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์— ๊ฐ๊ฐ ์ŠคํŠธ๋ฆผ์„ ์—ด์–ด ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
  • ์ง€์—ฐ ๊ฐ€๋Šฅ
    ๐Ÿถ ์ŠคํŠธ๋ฆผ ๋‚ด์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ชจ๋‘ ์ „์†ก๋˜๊ธฐ ์ „๊นŒ์ง€ ํ”„๋กœ๊ทธ๋žจ์ด ์ง€์—ฐ ์ƒํƒœ์— ๋น ์งˆ ์ˆ˜ ์žˆ๋‹ค.

์ŠคํŠธ๋ฆผ ๋ถ„๋ฅ˜

์šฉ๋„์— ์˜ํ•œ ๋ถ„๋ฅ˜

  • 1์ฐจ ์ŠคํŠธ๋ฆผ : ๋””๋ฐ”์ด์Šค์— ์ง์ ‘ ์—ฐ๊ฒฐ๋˜๋Š” ์ŠคํŠธ๋ฆผ
  • 2์ฐจ ์ŠคํŠธ๋ฆผ : 1์ฐจ ์ŠคํŠธ๋ฆผ๊ณผ ์—ฐ๊ฒฐ์„ ํ†ตํ•ด ๋””๋ฐ”์ด์Šค์™€ ์—ฐ๊ฒฐ๋˜๋Š” ์ŠคํŠธ๋ฆผ

์ „์†ก ๋ฐฉํ–ฅ์— ์˜ํ•œ ๋ถ„๋ฅ˜

  • ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ : ๋””๋ฐ”์ด์Šค๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์˜ค๋Š” ์ŠคํŠธ๋ฆผ
  • ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ : ๋””๋ฐ”์ด์Šค๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์ŠคํŠธ๋ฆผ

์ „์†ก๋‹จ์œ„์— ์˜ํ•œ ๋ถ„๋ฅ˜

  • ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ : 1 Byte ๋‹จ์œ„๋กœ ์ž…๋ ฅ, ์ถœ๋ ฅํ•˜๋Š” ์ŠคํŠธ๋ฆผ
  • ๋ฌธ์ž ์ŠคํŠธ๋ฆผ : ํ•œ ๋ฌธ์ž(2 Byte) ๋‹จ์œ„๋กœ ์ž…๋ ฅ, ์ถœ๋ ฅํ•˜๋Š” ์ŠคํŠธ๋ฆผ

๋ณด์กฐ ์ŠคํŠธ๋ฆผ

  • ์ŠคํŠธ๋ฆผ์˜ ๊ธฐ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ค๊ฑฐ๋‚˜ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€์‹œํ‚ค๋ฉฐ, ์ง์ ‘์ ์ธ ๋ฐ์ดํ„ฐ ์ž…์ถœ๋ ฅ์„ ํ•  ์ˆ˜ ์—†๋‹ค.

๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ

  • ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ ์ „์†ก (์„ฑ๋Šฅ์€ ์šฐ์ˆ˜ํ•˜๋‚˜ ํŒŒ์ผ๋กœ ์ €์žฅ์‹œ ๊ฒฐ๊ณผ ํ™•์ธ ๋ถˆ๊ฐ€ํ•จ)
  • ํŒŒ์ผ(๋™์˜์ƒ, ์ด๋ฏธ์ง€, ์Œ์•…)์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ์— ์ ํ•ฉ
  • ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ์˜ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค๋Š” InputStream, OutputStream

๋ฐ”์ดํŠธ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ : OutputStream

  • ๋ฐ”์ดํŠธ ๊ธฐ๋ฐ˜ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์˜ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค, ์ถ”์ƒ ํด๋ž˜์Šค

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Main {
    public static void main(String[] args) throws IOException {
        OutputStream os = new FileOutputStream("test.bin");
        byte[] array = {10,20,30,40,50};
        os.write(array, 1, 4); // ๋ฐฐ์—ด ์ธ๋ฑ์Šค 1๋ถ€ํ„ฐ 4๊ฐœ ์ถœ๋ ฅ
        os.flush(); // write() ์ดํ›„ ๋ฐ˜๋“œ์‹œ ๋ฒ„ํผ๋ฅผ ๋น„์šฐ๋Š” ๋™์ž‘์ด ํ•„์š” !!
        os.close(); // ์ž์› ๋ฐ˜๋‚ฉ ๋ฐ ์ŠคํŠธ๋ฆผ ๋‹ซ๊ธฐ
    }
}
  • write(int a, int b) : ๋ฐฐ์—ด ์ธ๋ฑ์Šค a๋ถ€ํ„ฐ b๊ฐœ ์ถœ๋ ฅ
  • โญ flush() : ๋ฒ„ํผ ๋น„์›€. write() ์ดํ›„ ๋ฐ˜๋“œ์‹œ ๋ฒ„ํผ ๋น„์›Œ์•ผ ํ•จ
  • โญ close() : ์ŠคํŠธ๋ฆผ ๋‹ซ๊ธฐ(์ž์› ๋ฐ˜๋‚ฉ)

๋ฐ”์ดํŠธ ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ : InputStream

  • ๋ฐ”์ดํŠธ ๊ธฐ๋ฐ˜ ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์˜ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค, ์ถ”์ƒ ํด๋ž˜์Šค

1๋ฐ”์ดํŠธ์”ฉ ์ฝ๊ธฐ

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        InputStream is = new FileInputStream("test.bin");
        byte[] buffer = new byte[100];
        while(true) {
            int data = is.read(); // 1๋ฐ”์ดํŠธ์”ฉ ์ฝ๊ธฐ
            if(data == -1) break; // ํŒŒ์ผ์˜ ๋์— ๋„๋‹ฌ
            System.out.print(data + " ");
        }
        is.close();
    }
}
20 30 40 50
1๋ฐ”์ดํŠธ์”ฉ ์ฝ๊ธฐ ๋•Œ๋ฌธ์— ํŒŒ์ผ ํฌ๊ธฐ๊ฐ€ ํด ๊ฒฝ์šฐ, ๊ต‰์žฅํžˆ ๋น„ํšจ์œจ์ ์ž„.

๋ฐฐ์—ด์˜ ๊ธธ์ด๋งŒํผ ์ฝ๊ธฐ

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        InputStream is = new FileInputStream("test.bin");
        byte[] buffer = new byte[100];
        while(true) {
            int readByteNum = is.read(buffer); // ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•œ buffer์— ๊ฐ’์„ ๋‹ด์Œ
            if(readByteNum == -1) break;
            for(int i = 0; i < readByteNum; i++) {
                System.out.print(buffer[i] + " ");
        }
        is.close(); // ์ฝ๊ธฐ ์œ„ํ•ด ์—ด์–ด๋‘” ์ŠคํŠธ๋ฆผ ๋‹ซ์Œ(๋ฉ”๋ชจ๋ฆฌ์— ์ž์› ๋ฐ˜๋‚ฉ)
    }
}
byte[]์„ ์ƒ์„ฑํ•˜๊ณ  ๊ทธ ํฌ๊ธฐ๋งŒํผ read()์— ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•˜์—ฌ int readByteNum์— ๋‹ด๋Š”๋‹ค.
int readByteNum์€ ๋ฐฐ์—ด์„ ๋ช‡ ๊ฐœ ์†Œ๋ชจํ–ˆ๋Š”์ง€๋ฅผ ์ฒดํฌํ•˜๋Š” ์šฉ๋„์ด๋ฉฐ, ์žํˆฌ๋ฆฌ ๊ณต๊ฐ„๊นŒ์ง€ ๋‹ด๊ณ  while๋ฌธ ํƒˆ์ถœ

๋ฌธ์ž ์ŠคํŠธ๋ฆผ

Writer

  • ๋ฌธ์ž์—ด ๊ธฐ๋ฐ˜ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์˜ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค, ์ถ”์ƒ ํด๋ž˜์Šค

write(int c) ๋ฉ”์†Œ๋“œ

public static void main(String[] args) throws Exception {
		Writer writer = new FileWriter("test.txt");
		char[] data = "๊ณฐ๋Œ์ด์‚ฌ์œก์‚ฌ".toCharArray();
		for(int i=0; i<data.length; i++) {
			writer.write(data[i]);
		}
		writer.flush();
		writer.close();
	}

write(char[] c) ๋ฉ”์†Œ๋“œ

public static void main(String[] args) throws Exception {
		Writer writer = new FileWriter("test.txt");
		char[] data = "๊ณฐ๋Œ์ด์‚ฌ์œก์‚ฌ".toCharArray();
		writer.write(data);

		writer.flush();
		writer.close();
	}

write(char[] c, int off, int len) ๋ฉ”์†Œ๋“œ

public static void main(String[] args) throws Exception {
		Writer writer = new FileWriter("test.txt");
		//char[] data = "๊ณฐ๋Œ์ด์‚ฌ์œก์‚ฌ".toCharArray();
		String data = "๊ณฐ๋Œ์ด์‚ฌ์œก์‚ฌ";
		writer.write(data, 1, 2);

		writer.flush();
		writer.close();
	}

Reader

  • ๋ฌธ์ž ๊ธฐ๋ฐ˜ ์ž…๋ ฅ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค, ์ถ”์ƒ ํด๋ž˜์Šค

read()

  • ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋ถ€ํ„ฐ ํ•œ ๊ฐœ์˜ ๋ฌธ์ž(2๋ฐ”์ดํŠธ)๋ฅผ ์ฝ๊ณ  4๋ฐ”์ดํŠธ(int)๋กœ ๋ฆฌํ„ด ํ•ฉ๋‹ˆ๋‹ค.
  • read() ๋ฉ”์†Œ๋“œ๊ฐ€ ๋ฆฌํ„ดํ•œ int ๊ฐ’์„ charํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋ฉด ์ฝ์€ ๋ฌธ์ž์—ด ํ™•์ธ ๊ฐ€๋Šฅ
public static void main(String[] args) throws Exception {
    Reader reader = new FileReader("src/test.txt");
    int readData;
    while( true ) {
        readData = reader.read();
        if(readData == -1) break;
        System.out.print((char)readData);
    }
    reader.close();
}

read(char[] cbuf) ๋ฉ”์†Œ๋“œ

์ž…๋ ฅ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋ถ€ํ„ฐ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง„ ๋ฌธ์ž ๋ฐฐ์—ด์˜ ๊ธธ์ด๋งŒํผ ๋ฌธ์ž๋ฅผ ์ฝ๊ณ  ๋ฐฐ์—ด์— ์ €์žฅ ํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ , ์ฝ์€ ๋ฌธ์ž ์ˆ˜๋ฅผ ๋ฆฌํ„ด ํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ์ฝ์€ ๋ฌธ์ž ์ˆ˜๊ฐ€ ๋ฐฐ์—ด ๊ธธ์ด ๋ณด๋‹ค ์ž‘์„ ๊ฒฝ์šฐ ์ฝ์€ ์ˆ˜๋งŒํผ๋งŒ ๋ฆฌํ„ด ํ•ฉ๋‹ˆ๋‹ค.

public static void main(String[] args) throws Exception {
    Reader reader = new FileReader("src/test.txt");
    int readCharNo;
    char[] cbuf = new char[10];
    String data = "";
    while( true ) {
        readCharNo = reader.read(cbuf);
        if(readCharNo == -1) break;
        data += new String(cbuf, 0, readCharNo);
    }
    System.out.println(data);

    reader.close();
}

ํŒŒ์ผ ์ž…์ถœ๋ ฅ

File ํด๋ž˜์Šค

  • ํŒŒ์ผ์ด๋‚˜ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ถ”์ƒํ™”ํ•œ ํด๋ž˜์Šค, ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ์ž…์ถœ๋ ฅ ๋Œ€์ƒ
File file = new File("C:\\Temp\\file.txt");
File file = new File("C:/Temp/file.txt");
public static void main(String[] args) throws URISyntaxException, IOException {
    File temp = new File("src");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd   a   HH:mm");
        File[] contents = temp.listFiles();
        System.out.println("๋‚ ์งœ              ์‹œ๊ฐ„         ํ˜•ํƒœ       ํฌ๊ธฐ    ์ด๋ฆ„");
        System.out.println("-------------------------------------------------------------------");
        for(File file : contents) {
            System.out.print(sdf.format(new Date(file.lastModified()))); // ํŒŒ์ผ์˜ ์ตœ์ข… ๊ฐฑ์‹  ์‹œ๊ฐ„
            if(file.isDirectory()) {
                System.out.print("\t<DIR>\t\t\t" + file.getName()); // ํŒŒ์ผ ์ด๋ฆ„์„ ์ŠคํŠธ๋ง์œผ๋กœ ๋ฐ˜ํ™˜
            } else {
                System.out.print("\t\t\t" + file.length() + "\t" + file.getName());
            }
            System.out.println();
        }
}

FileInputStream

  • ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ์ฝ์–ด ๋“ค์ผ ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ๋ฐ”์ดํŠธ ๊ธฐ๋ฐ˜ ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ
public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream("src/ํŒŒ์ผ์ธํ’‹์ŠคํŠธ๋ฆผ/FileInputStreamEx.java");
            int data;
            while ( (data = fis.read() ) != -1 ) {
                System.out.write(data);
            }
            fis.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

FileOutputStream

  • ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์ผ์— ์ €์žฅ ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ”์ดํŠธ ๊ธฐ๋ฐ˜ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ
  • ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ์ €์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋ฆผ, ์˜ค๋””์˜ค, ๋น„๋””์˜ค, ํ…์ŠคํŠธ ๋“ฑ ๋ชจ๋“  ์ข…๋ฅ˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์ผ๋กœ ์ €์žฅ ๊ฐ€๋Šฅ
public static void main(String[] args) throws IOException {
    String originalFileName = "src/ํŒŒ์ผ์•„์›ƒํ’‹์ŠคํŠธ๋ฆผ/house.jpg";
    String targetFileName = "src/house.jpg";

    FileInputStream fis = new FileInputStream(originalFileName);
    FileOutputStream fos = new FileOutputStream(targetFileName);

    int readByteNo;
    byte[] readBytes = new byte[100];
    while( (readByteNo = fis.read(readBytes)) != -1 ) {
        fos.write(readBytes, 0, readByteNo);
    }

    fos.flush();
    fos.close();
    fis.close();

    System.out.println("๋ณต์‚ฌ๊ฐ€ ์ž˜ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
}

FileInputStream๊ณผ Scanner ํ™œ์šฉ

  • Scanner ์ž…๋ ฅ์œผ๋กœ ํŒŒ์ผ์„ ์ฝ์Šต๋‹ˆ๋‹ค.
  • ์Šค์บ๋„ˆ์˜ hasNextLine()์œผ๋กœ ์ฝ์„ ๋‚ด์šฉ ์žˆ๋Š”์ง€ ํ™•์ธ
package ํŒŒ์ผ์ž…์ถœ๋ ฅ;

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

public class FileInput {
    public static void main(String[] args) {
        FileInputStream inputStream = null;

        try {
            inputStream = new FileInputStream("src/ํŒŒ์ผ์ž…์ถœ๋ ฅ/chiken.txt");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        Scanner sc = new Scanner(inputStream);
        while(sc.hasNextLine()) {
            String line = sc.nextLine();
            System.out.println(line);
        }
    }
}

๋ณด์กฐ ์ŠคํŠธ๋ฆผ

โญ์„ฑ๋Šฅ ํ–ฅ์ƒ ๋ณด์กฐ์ŠคํŠธ๋ฆผ

BufferedOutputStream, BufferedWriter

BufferedInputStream, BufferedReader

๋ฒ„ํผ ์‚ฌ์šฉ ์ „!!!

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedInputStream bis = null; // ์ž…๋ ฅ ๋ณด์กฐ ์ŠคํŠธ๋ฆผ
        BufferedOutputStream bos = null; // ์ถœ๋ ฅ ๋ณด์กฐ ์ŠคํŠธ๋ฆผ

        int data = -1; // ๋” ์ด์ƒ ์ฝ์„ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์Œ์„ ์˜๋ฏธ
        long start = 0; // ๊ฒฝ๊ณผ ์‹œ๊ฐ„ ๊ณ„์‚ฐ์„ ์œ„ํ•œ ๋ณ€์ˆ˜
        long end = 0; // ๊ฒฝ๊ณผ ์‹œ๊ฐ„ ๊ณ„์‚ฐ์„ ์œ„ํ•œ ๋ณ€์ˆ˜

        fis = new FileInputStream("src/์„ฑ๋Šฅํ–ฅ์ƒ๋ณด์กฐ์ŠคํŠธ๋ฆผ0210/night.jpg");
        bis = new BufferedInputStream(fis);
        fos = new FileOutputStream("clone_night.jpg");
        start = System.currentTimeMillis(); // 1970๋…„ 1์›” 1์ผ 0์‹œ 0๋ถ„ 0์ดˆ๋กœ ๋ถ€ํ„ฐ ํ˜„์žฌ๊นŒ์ง€ ๊ฒฝ๊ณผ ์‹œ๊ฐ„์„ ๋ฐ€๋ฆฌ์ดˆ๋กœ ํ‘œ์‹œ
        while((data = bis.read()) != -1) {
            fos.write(data);
        }
        fos.flush();
        end = System.currentTimeMillis();
        fos.close();
        bis.close();
        fis.close();
        System.out.println("๋ฒ„ํผ ์‚ฌ์šฉ์„ ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ์‹œ๊ฐ„ : " + (end - start) + "ms");
๋ฒ„ํผ ์‚ฌ์šฉ์„ ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ์‹œ๊ฐ„ : 588ms

๋ฒ„ํผ ์‚ฌ์šฉ ํ›„!!!!

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedInputStream bis = null; // ์ž…๋ ฅ ๋ณด์กฐ ์ŠคํŠธ๋ฆผ
        BufferedOutputStream bos = null; // ์ถœ๋ ฅ ๋ณด์กฐ ์ŠคํŠธ๋ฆผ

        int data = -1; // ๋” ์ด์ƒ ์ฝ์„ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์Œ์„ ์˜๋ฏธ
        long start = 0; // ๊ฒฝ๊ณผ ์‹œ๊ฐ„ ๊ณ„์‚ฐ์„ ์œ„ํ•œ ๋ณ€์ˆ˜
        long end = 0; // ๊ฒฝ๊ณผ ์‹œ๊ฐ„ ๊ณ„์‚ฐ์„ ์œ„ํ•œ ๋ณ€์ˆ˜

        // ๋ฒ„ํผ ๋ณด์กฐ ์ŠคํŠธ๋ฆผ ๋‘์–ด์„œ ์„ฑ๋Šฅ ์ธก์ •
        fis = new FileInputStream("src/์„ฑ๋Šฅํ–ฅ์ƒ๋ณด์กฐ์ŠคํŠธ๋ฆผ0210/night.jpg");
        bis = new BufferedInputStream(fis);
        fos = new FileOutputStream("clone_night.jpg");
        bos = new BufferedOutputStream(fos);
        start = System.currentTimeMillis();
        while((data = bis.read()) != -1) {
            bos.write(data);
        }
        bos.flush();
        end = System.currentTimeMillis();
        fos.close();
        bis.close();
        fis.close();
        bos.close();
        System.out.println("๋ฒ„ํผ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ์‹œ๊ฐ„ : " + (end - start) + "ms");
    }
}
๋ฒ„ํผ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ์‹œ๊ฐ„ : 1ms

๐Ÿธ ์„ฑ๋Šฅ์ด ๊ต‰์žฅํžˆ ์ข‹์•„์ง„ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค!

๊ธฐ๋ณธ ํƒ€์ž… ๋ณด์กฐ์ŠคํŠธ๋ฆผ

  • Scanner๋กœ ๋Œ€์ฒด ๊ฐ€๋Šฅ
  • ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ์€ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ์ž…์ถœ๋ ฅ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ž๋ฐ”์˜ ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž… ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ.
    ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋ณด์กฐ ์ŠคํŠธ๋ฆผ์„ ์—ฐ๊ฒฐํ•ด ์‚ฌ์šฉํ•จ.

DataInputStream, DataOutputStream

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("๊ธฐ๋ณธํƒ€์ž…๋ณด์กฐ์ŠคํŠธ๋ฆผ.bin");
        DataOutputStream dos = new DataOutputStream(fos); // ํ”„๋กœ๊ทธ๋žจ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋ณธ ํƒ€์ž…์„ ์ง€์›ํ•˜๋Š” ๋ณด์กฐ ์ŠคํŠธ๋ฆผ
        dos.writeUTF("์ด์ˆœ์‹ "); // ๋ฌธ์ž์—ด ๋ฉ”์†Œ๋“œ
        dos.writeDouble(99.8); // ์‹ค์ˆ˜ํ˜• ๋ฉ”์†Œ๋“œ
        dos.writeInt(1); // ์ •์ˆ˜ํ˜• ๋ฉ”์†Œ๋“œ

        dos.writeUTF("ํ•œ์„๋ด‰");
        dos.writeDouble(64.5);
        dos.writeInt(2);

        dos.writeUTF("ํ™๊ธธ๋™");
        dos.writeDouble(84.2);
        dos.writeInt(3);

        dos.flush();
        dos.close();

        FileInputStream fis = new FileInputStream("๊ธฐ๋ณธํƒ€์ž…๋ณด์กฐ์ŠคํŠธ๋ฆผ.bin");
        DataInputStream dis = new DataInputStream(fis);

        for(int i = 0; i < 3; i++) {
            String name = dis.readUTF(); // ๋ฌธ์ž์—ด์„ ์ฝ์Œ
            double score = dis.readDouble();
            int num = dis.readInt();
            System.out.println(name + " : " + score + num);
        }
    }
}

โญ๊ฐ์ฒด ์ž…์ถœ๋ ฅ ๋ณด์กฐ์ŠคํŠธ๋ฆผ

    • ์ง๋ ฌํ™”?
      โžก๏ธ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ์ฒด๋ฅผ ํŒŒ์ผ์ด๋‚˜ ๋„คํŠธ์›Œํฌ๋กœ ์ „์†กํ•˜๊ธฐ ์œ„ํ•ด์„œ ์—ฐ์†์ ์ธ ๋ฐ”์ดํŠธ ํ˜•ํƒœ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ
    • ์ง๋ ฌํ™”๋œ ๋ฐ”์ดํŠธ๋ฅผ ๊ฐ์ฒด๋กœ ๋ณต์›ํ™”๋Š” ๊ฒƒ์„ '์—ญ์ง๋ ฌํ™”'๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
    • Serializable
์ง๋ ฌํ™” ์ž์ฒด๋Š” ์ž˜ ์“ฐ์ด์ง€ ์•Š์ง€๋งŒ(๊ฐ์ฒด ์ง๋ ฌํ™”๋Š” JAVA๋ผ๋ฆฌ๋งŒ ํ˜ธํ™˜๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณดํ†ต JSON์„ ์ด์šฉํ•จ)
์ง๋ ฌํ™”๋ฅผ ์“ฐ๋Š” ํŒจํ„ด์€ ๋ฐฑ์—”๋“œ์—์„œ ์ž์ฃผ ์“ฐ์ด๋ฏ€๋กœ ์ต์ˆ™ํ•ด์ง€๋„๋ก ํ•˜์ž.
  • ๊ฐ์ฒด๋ฅผ ์ง๋ ฌํ™” ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Serializable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์† ๋ฐ›์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ํด๋ž˜์Šค ๋‚ด์— ํฌํ•จ๋œ ํ•„๋“œ ์ค‘ ์ง๋ ฌํ™” ๋Œ€์ƒ์—์„œ ์ œ์™ธํ•  ๋‚ด์šฉ์ด ์žˆ์œผ๋ฉด transient ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
import java.io.Serializable;
import java.util.Date;

public class Board implements Serializable {
    private int number;
    private String title;
    private String content;
    private String writer;
    private Date date;
    transient String test;

    public Board(int number, String title, String content, String writer, Date date) {
        this.number = number;
        this.title = title;
        this.content = content;
        this.writer = writer;
        this.date = date;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getWriter() {
        return writer;
    }

    public void setWriter(String writer) {
        this.writer = writer;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Main {
    public static void main(String[] args) throws IOException, ClassNotFoundException { // ๊ฐ์ฒด์•  ๋Œ€ํ•œ ์ง๋ ฌํ™”
        writeList();
        List<Board> list = readList();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for(Board e : list) {
            System.out.println("๋ฒˆํ˜ธ : " + e.getNumber());
            System.out.println("์ œ๋ชฉ : " + e.getTitle());
            System.out.println("๋‚ด์šฉ : " + e.getContent());
            System.out.println("์—ฐ์ถœ๊ฐ€ : " + e.getWriter());
            System.out.println("์‹œ๊ฐ„ : " + sdf.format(e.getDate()));
            System.out.println("------------------------------------");
        }
    }
    static void writeList() throws IOException {
        List<Board> list = new ArrayList<>();
        list.add(new Board(1, "๋‚˜๋Š” ์†”๋กœ","์—ฐ์•  ์˜ˆ๋Šฅ","๊น€๋•ก๋–™", new Date()));
        list.add(new Board(2, "๋ฌด์—‡์ด๋“  ๋ฌผ์–ด๋ณด์‚ด","์˜ˆ๋Šฅ","์ด๋•ก๋–™", new Date()));
        list.add(new Board(3, "์ฐจํŠธ๋ฅผ ๋‹ฌ๋ฆฌ๋Š” ๋‚จ์ž","์ฐจํŠธ ์˜ˆ๋Šฅ","๋ฐ•๋•ก๋–™", new Date()));

        FileOutputStream fos = new FileOutputStream("board.bin");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(list); // ๊ฐ์ฒด ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์„ ์ด์šฉํ•ด list๋ฅผ ์ถœ๋ ฅ
        oos.flush();
        oos.close();
    }
    static List<Board> readList() throws IOException, ClassNotFoundException {
        FileInputStream fis = new FileInputStream("board.bin");
        ObjectInputStream ois = new ObjectInputStream(fis);
        List<Board> list = (List<Board>)ois.readObject(); // ํŒŒ์ผ์˜ ๋ฐ”์ด๋„ˆ๋ฆฌ๋ฅผ ์—ญ์ง๋ ฌํ™”
        return list;
    }
}
๋ฒˆํ˜ธ : 1
์ œ๋ชฉ : ๋‚˜๋Š” ์†”๋กœ
๋‚ด์šฉ : ์—ฐ์•  ์˜ˆ๋Šฅ
์—ฐ์ถœ๊ฐ€ : ๊น€๋•ก๋–™
์‹œ๊ฐ„ : 2023-02-10
------------------------------------
๋ฒˆํ˜ธ : 2
์ œ๋ชฉ : ๋ฌด์—‡์ด๋“  ๋ฌผ์–ด๋ณด์‚ด
๋‚ด์šฉ : ์˜ˆ๋Šฅ
์—ฐ์ถœ๊ฐ€ : ์ด๋•ก๋–™
์‹œ๊ฐ„ : 2023-02-10
------------------------------------
๋ฒˆํ˜ธ : 3
์ œ๋ชฉ : ์ฐจํŠธ๋ฅผ ๋‹ฌ๋ฆฌ๋Š” ๋‚จ์ž
๋‚ด์šฉ : ์ฐจํŠธ ์˜ˆ๋Šฅ
์—ฐ์ถœ๊ฐ€ : ๋ฐ•๋•ก๋–™
์‹œ๊ฐ„ : 2023-02-10
------------------------------------

 

profile

GiantStepDEV

@kongmi

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!