BackEnd๐Ÿงต

TIL - ํŒŒ์ผ ์ž…/์ถœ๋ ฅ

hae02y 2023. 5. 10. 19:17
๋ฐ˜์‘ํ˜•

 

๐Ÿ‘€Today...


 

 

โ˜•์˜ค๋Š˜์˜ TIL(Today I Learn)


์ž…์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ

์ž๋ฐ”์—์„œ๋Š” ์ž…์ถœ๋ ฅ์„ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•ด InputStream , OutputStream์„ ์ œ๊ณตํ•œ๋‹ค. ์ŠคํŠธ๋ฆผ์€ ๋‹จ๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ• ์ˆ˜์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์„ ๋™์‹œ์— ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฐ๊ฐ์˜ ์ŠคํŠธ๋ฆผ์ด ํ•„์š”ํ•˜๋‹ค. ์ด๋Ÿฌํ•œ Input / Output Stream์€ ์ถ”์ƒํด๋ž˜์Šค์ด๊ณ  ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•˜์—ฌ ํŒŒ์ผ, ๋„คํŠธ์›Œํฌ, ๋ฉ”๋ชจ๋ฆฌ, ์˜ค๋””์˜ค, ํ”„๋กœ์„ธ์Šค ๋“ฑ ๋‹ค์–‘ํ•œ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ• ์ˆ˜์žˆ๋‹ค. ๋ฐ‘์—์„œ ์„ค๋ช…ํ•˜๊ณ ์ž ํ•˜๋Š” ํŒŒ์ผ์ž…์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ๋„ ์—ฌ๊ธฐ์— ์†ํ•œ๋‹ค. ์ž๋ฐ”์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” I/O๊ธฐ๋Šฅ์€ java.ioํŒจํ‚ค์ง€์—์„œ ์ œ๊ณต๋œ๋‹ค. Input์€ ๋ง๊ทธ๋Œ€๋กœ ์™ธ๋ถ€์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜๊ณ , Output์€ ์™ธ๋ถ€๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

 

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

# FileInputStream

import java.io.FileInputStream; //java.io ํŒจํ‚ค์ง€
  
public class FileInputStreamExample {
    public static void main(String args[])
    {
        try {
            FileInputStream fileInput = new FileInputStream("example.txt"); //์ฝ์–ด์˜ฌ ํŒŒ์ผ๋ช…
            int i = 0;
            while ((i = fileInput.read()) != -1) { //fileInput.read()์˜ ๋ฆฌํ„ด๊ฐ’์„ i์— ์ €์žฅํ•œ ํ›„, ๊ฐ’์ด -1์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
                System.out.print((char)i);
            }
            fileInput.close();					//file์ž…์ถœ๋ ฅํ›„์— close๋กœ ๋‹ซ์•„์ค˜์•ผํ•œ๋‹ค.
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
๋”๋ณด๊ธฐ

 

๋ฐ์ดํ„ฐ์˜ ๋์— ๋„๋‹ฌํ•˜๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

BufferedInputStream์ด๋ผ๋Š” ๋ณด์กฐ ์ŠคํŠธ๋ฆผ์„ ์‚ฌ์šฉํ•˜๋ฉด ์„ฑ๋Šฅ์ด ํ–ฅ์ƒ๋œ๋‹ค.

 

# FileOutputStream

import java.io.FileOutputStream;
  
public class FileOutputStreamExample {
    public static void main(String args[]) {
        try {
            FileOutputStream fileOutput = new FileOutputStream("Example.txt");
            String word = "gil";

            byte b[] = word.getBytes();
            fileOutput.write(b);
            fileOutput.close();
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด gil์ด๋ผ๋Š” ๋ฌธ์ž์—ด์ด ์ž…๋ ฅ๋œ Example.txt ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ๋‹ค.

 

 

๋ฌธ์ž ๊ธฐ๋ฐ˜ ์ŠคํŠธ๋ฆผ

๋ฐ”์ดํŠธ๊ธฐ๋ฐ˜์€ ์ž…์ถœ๋ ฅ๋‹จ์œ„๊ฐ€ 1byte์ธ๋ฐ, JAVA์—์„œ์˜ char ํƒ€์ž…์€ 2byte์ด๋‹ค. ์ด๋•Œ๋ฌธ์— ์ž๋ฐ”์—์„œ๋Š” ๋ฌธ์ž๊ธฐ๋ฐ˜ ์ŠคํŠธ๋ฆผ์„ ์ œ๊ณตํ•œ๋‹ค. ๋ฌธ์ž๊ธฐ๋ฐ˜ ์ŠคํŠธ๋ฆผ๊ณผ ๊ทธ ํ•˜์œ„ํด๋ž˜์Šค๋Š” ์—ฌ๋Ÿฌ ์ธ์ฝ”๋”ฉ๊ณผ ์œ ๋‹ˆ์ฝ”๋“œ(์ž๋ฐ”์—์„œ ์‚ฌ์šฉ)๊ฐ„์˜ ๋ณ€ํ™˜์„ ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค. ์ฆ‰ ํ•œ๊ธ€๋„ ์ž…๋ ฅ์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค.

 

# FileReader

์ธ์ฝ”๋”ฉ => ์œ ๋‹ˆ์ฝ”๋“œ ๋ณ€ํ™˜

public class FileReaderExample {
    public static void main(String args[]) {
        try {
            String fileName = "Example.txt";
            FileReader file = new FileReader(fileName);

            int data = 0;

            while((data=file.read()) != -1) {
                System.out.print((char)data);
            }
            file.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

BufferedReader๋กœ ์„ฑ๋Šฅ ๊ฐœ์„ ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

#FileWriter

์œ ๋‹ˆ์ฝ”๋“œ => ์ธ์ฝ”๋”ฉ ๋ณ€ํ™˜

public class FileWriterExample {
    public static void main(String args[]) {
        try {
            String fileName = "Example.txt";
            FileWriter writer = new FileWriter(fileName);

            String str = "FileWriter~~!";
            writer.write(str);
            writer.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

 

File ํด๋ž˜์Šค

File ํด๋ž˜์Šค๋กœ ํŒŒ์ผ๊ณผ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 

import java.io.*;

public class FileExample {
    public static void main(String args[]) throws IOException {
            File file = new File("./", "fileexample.txt"); //ํŒŒ์ผ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
            file.createNewFile(); // ํŒŒ์ผ์ƒ์„ฑ ๋ฉ”์„œ๋“œ

            System.out.println(file.getPath());
            System.out.println(file.getParent());
            System.out.println(file.getCanonicalPath());
            System.out.println(file.canWrite());
    }
}

[ref. Fileclass]

 

 

 

2023.05.12 - [Languages๐Ÿ”ฅ/Java] - ์ ˆ๋Œ€๊ฒฝ๋กœ / ์ƒ๋Œ€๊ฒฝ๋กœ


 

๋ฐ˜์‘ํ˜•