ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Java ์‘์šฉ] 6. ํŒŒ์ผ ์กฐ์ž‘
    Dev/Java 2023. 3. 26. 23:44
    728x90

    ๐Ÿ“˜ ์‘์šฉ 6์žฅ ํŒŒ์ผ ์กฐ์ž‘

    ํŒŒ์ผ ์กฐ์ž‘์˜ ๊ธฐ๋ณธ ์ˆœ์„œ

    ๐Ÿ’ก
    1. ํŒŒ์ผ์„ ์—ฐ๋‹ค 2. ํŒŒ์ผ์„ ์ฝ๊ฑฐ๋‚˜ ์“ด๋‹ค 3. ํŒŒ์ผ์„ ๋‹ซ๋Š”๋‹ค

    FileWriter

    import java.io.FileWriter;
    import java.io.IOException;
    
    public class Main {
        public static void main(String[] args) throws IOException {
            // ํŒŒ์ผ ์—ด๊ธฐ, append ๋ชจ๋“œ
            FileWriter fw = new FileWriter("data.txt", true);
            
            // ๋‚ด์šฉ ์ž‘์„ฑ
            fw.write("Hello World");
            fw.flush(); // ์‹ค์ œ ํŒŒ์ผ ์“ฐ๊ธฐ ์‹œ์ž‘
            fw.close(); // ํŒŒ์ผ ๋‹ซ๊ธฐ
        }
    }

    FileReader

    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class Main {
    
        public static void main(String[] args) throws IOException {
            FileReader fr = new FileReader("data.txt");
            
            int ch = fr.read(); //char๋ฅผ ์ฝ์–ด์˜ค๋Š”๋ฐ int๋ฅผ ์ €์žฅํ•จ
            System.out.println(ch);
        }    
    }

    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class Main {
    
        public static void main(String[] args) throws IOException {
            FileReader fr = new FileReader("data.txt");
            
            int ch = fr.read(); //char๋ฅผ ์ฝ์–ด์˜ค๋Š”๋ฐ int๋ฅผ ์ €์žฅํ•จ
            
            while (ch != -1) {
                System.out.println((char) ch);
                ch = fr.read();
            }
        }
    }

    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import javax.xml.crypto.Data;
    
    public class Main {
    
        public static void main(String[] args) throws IOException {
            System.out.println(fileRead("Data.txt"));
        }
        
        public static String fileRead(String fileName) throws IOException {
            String result = "";
            FileReader fr = new FileReader("data.txt");
            
            int ch = fr.read(); //char๋ฅผ ์ฝ์–ด์˜ค๋Š”๋ฐ int๋ฅผ ์ €์žฅํ•จ
            
            while (ch != -1) {
                result += (char) ch;
                ch = fr.read();
            }
            return result;
        }
        
        public static void fileWriterCode() throws IOException {
            // ํŒŒ์ผ ์—ด๊ธฐ, append ๋ชจ๋“œ
            FileWriter fw = new FileWriter("data.txt", true);
            
            // ๋‚ด์šฉ ์ž‘์„ฑ
            fw.write("Hello World");
            fw.flush(); // ํŒŒ์ผ ์“ฐ๊ธฐ ์‹œ์ž‘
            fw.close(); // ํŒŒ์ผ ๋‹ซ๊ธฐ
        }
        
    }

    ๋ฐ”์ด๋„ˆ๋ฆฌํŒŒ์ผ์˜ ์กฐ์ž‘

    ํŒŒ์ผ์—๋Š” ํฌ๊ฒŒ ๋‘ ์ข…๋ฅ˜์˜ ํŒŒ์ผ์ด ์žˆ๋‹ค

    ๐Ÿ’ก
    ํ…์ŠคํŠธ ํŒŒ์ผ
    • ๋ฌธ์ž๋กœ ๊ตฌ์„ฑ
    • ๋ฉ”๋ชจ์žฅ, ์†Œ์Šค์ฝ”๋“œ, HTML ๋“ฑ์ด ํ•ด๋‹น
    • FileReader, FileWriter ์‚ฌ์šฉ

    ๋ฐ”์ด๋„ˆ๋ฆฌ ํŒŒ์ผ

    • ๋ฌธ์ž์™€ ๋ฐ์ดํ„ฐ(๋ฐ”์ดํŠธ ๋ฐฐ์—ด)
    • Excel, Java์˜ class ํŒŒ์ผ, ์ด๋ฏธ์ง€ ํŒŒ์ผ, ๋™์˜์ƒ ํŒŒ์ผ, ์Œ์•… ํŒŒ์ผ ๋“ฑ
    • FileInputStream, FileOutputStream ์‚ฌ์šฉ

    ํŒŒ์ผ์— 2์ง„์ˆ˜ 01000001์„ ์ถ”๊ฐ€๋กœ ์ €์žฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ

    public class Main {
    
        public static void main(String[] args) throws IOException {
            FileOutputStream fos = new FileOutputStream("save.txt", true);
            fos.write(65); // A
            fos.write(66); // B
            fos.flush();
            fos.close();
        }
    }

    close()๋ฅผ ์•ˆ ํ–ˆ์„ ๊ฒฝ์šฐ

    ๐Ÿ’ก
    ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์ค‘์ธ ์ƒํƒœ์—์„œ ํŒŒ์ผ์„ ๋‹ค ์‚ฌ์šฉ ํ–ˆ์Œ์—๋„, ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋žจ์ด ํ•ด๋‹น ๊ทธ ํŒŒ์ผ์„ ์—ด ์ˆ˜ ์—†๋‹ค.

    ์˜ˆ์™ธ์ฒ˜๋ฆฌ

    ๐Ÿ’ก
    ์˜ฌ๋ฐ”๋ฅธ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์„œ ํŒŒ์ผ์„ ๋ฐ˜๋“œ์‹œ close() ํ•ด์•ผ ํ•œ๋‹ค. close()๋Š” ๋ฐ˜๋“œ์‹œ ์ฒ˜๋ฆฌ ๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ, finally ๋กœ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๊ณ , ํ•ด๋‹น close()์— ๋Œ€ํ•ด ๋‹ค์‹œ try-catch ํ•˜๋Š” ํ˜•ํƒœ๋กœ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค. (Lagacy ํ˜•ํƒœ)
    ๐Ÿ’ก
    Java8 ๋ถ€ํ„ฐ๋Š” Closeable์„ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๋Š” trye() ์—์„œ ์ž๋™์œผ๋กœ close() ํ•ด ์ค€๋‹ค.

    ์ŠคํŠธ๋ฆผ (Stream)

    ๐Ÿ’ก
    1. FileReader ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ๊ธˆ์”ฉ ์ฝ๊ฑฐ๋‚˜ ์“ฐ๋Š” API ์ž„ 2. ํ•œ๋ฒˆ์— 10GB์˜ ํŒŒ์ผ์„ ์ฝ์–ด๋ฒ„๋ฆฌ๊ฒŒ ๋˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ ๋ถ€์กฑ์ด ๋ฐœ์ƒ ํ•จ 3. ์ŠคํŠธ๋ฆผ์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ๊ธˆ์”ฉ ํ˜๋ ค๋ณด๋‚ด๊ณ  ์ฒ˜๋ฆฌํ•˜๋ฉด ์ด๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Œ 4. FileReader, FileWriter, FileInputStream, FileOutputStream ๋Š” ๋ชจ๋‘ ์ŠคํŠธ๋ฆผ ๊ณ„์—ด

    ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ŠคํŠธ๋ฆผ

    ๋ฐ์ดํ„ฐ์˜ ํ๋ฆ„์„ Stream์ด๋ผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

    ๋ฌธ์ž์—ด๋กœ๋ถ€ํ„ฐ ํ•œ ๊ธ€์ž์”ฉ ์ฝ๊ธฐ

    public class Main {
    
        public static void main(String[] args) throws IOException {
            String msg = "Hello World";
            Reader reader = new StringReader(msg);
        }
    }

    ๋ฐ”์ดํŠธ ๋ฐฐ์—ด์— ๊ฐ’์„ ์“ฐ๊ธฐ

    ํ‘œ์ค€ ์ž…์ถœ๋ ฅ

    ๐Ÿ’ก
    ํ‘œ์ค€ ์ถœ๋ ฅ : System.out => ๋ชจ๋‹ˆํ„ฐ ํ‘œ์ค€ ์ž…๋ ฅ : System.in => ํ‚ค๋ณด๋“œ

    ํ‘œ์ค€ ์ถœ๋ ฅ์€ ์ง€์ •๋œ ๊ฒƒ์ด ์•„๋‹Œ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

    ์ถœ๋ ฅ์„ data.txt๋กœ ๋ณ€๊ฒฝํ–ˆ๊ณ , data.txt ์ž…๋ ฅ์œผ๋กœ Main ํ”„๋กœ๊ทธ๋žจ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

    ํ•„ํ„ฐ

    ๐Ÿ’ก
    FilterReader, FilterWriter, FilterInputStream, FilterOutput ์„ ์ƒ์† ๋ฐ›๋Š” ํด๋ž˜์Šค๋“ค.
    ๐Ÿ’ก
    ๊ฐ ์ŠคํŠธ๋ฆผ๊ฐ„ ์—ฐ๊ฒฐ์ด ๋˜์–ด ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

    ๋ฒ„ํผ๋ง์„ ์œ„ํ•œ ํ•„ํ„ฐ

    ๐Ÿ’ก
    ๋ฌธ์ž์šฉ ํ•„ํ„ฐ : BufferedReader, BufferedWriter ๋ฐ”์ดํŠธ์šฉ ํ•„ํ„ฐ : BufferedInputStream, BufferedOutputStream

    ์ฒ˜๋ฆฌ์„ฑ๋Šฅ ํ–ฅ์ƒ

    • ํŒŒ์ผ์— ์“ฐ๊ฑฐ๋‚˜ ์ฝ์„๋•Œ ๋งˆ๋‹ค ๋งค๋ฒˆ ํ•˜๋“œ๋””์Šคํฌ๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ๊ฒƒ์€ ๋งค์šฐ ๋А๋ฆผ
    • ํ•œ ๋ฒˆ์— ๋ชจ๋“  ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ์“ฐ๋Š” ๊ฒƒ์ด ํšจ์œจ์ 
    • ๋ฒ„ํผ๋ง์€ ๋ฏธ๋ฆฌ ์ฝ์–ด ๋‘๊ฑฐ๋‚˜ ํ•œ ๋ฒˆ์— ์“ฐ๋Š” ์ž‘์—…
    • ๋”ฐ๋ผ์„œ ํŒŒ์ผ ์กฐ์ž‘์€ ๋ฒ„ํผ๋ง์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ํšจ์œจ์ 
    ๐Ÿ’ก
    FileReader์™€ BufferReader๋ฅผ ์—ฐ๊ฒฐํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

    ํŒŒ์ผ ์‹œ์Šคํ…œ์˜ ์กฐ์ž‘

    File file = new File("save.dat");

    ํŒŒ์ผ ์กฐ์ž‘ ๋ฉ”์„œ๋“œ

    ๐Ÿ’ก
    ์‚ญ์ œ : delete() ์ด๋ฆ„ ๋ณ€๊ฒฝ : renameTo(File dest) ์กด์žฌ ํ™•์ธ : exists() ํŒŒ์ผ์ธ์ง€? : isFile() ๋””๋ ‰ํ† ๋ฆฌ์ธ์ง€? : isDirectory() ์šฉ๋Ÿ‰ : length() ํด๋” ๋‚ด ํŒŒ์ผ๋“ค : listFiles() ๋ณต์‚ฌ ๊ธฐ๋Šฅ์€ ์ œ๊ณตํ•˜์ง€ ์•Š๋Š”๋‹ค.

    java.nio.file.Files

    ํŒŒ์ผ์— ๊ด€๋ จ๋œ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์€ Files ํด๋ž˜์Šค์—์„œ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค.

    ๐Ÿ’ก
    <์ œ๊ณต๋˜๋Š” ํƒœํ‘œ์ ์ธ ๊ธฐ๋Šฅ> ๋ณต์‚ฌ, ์ด๋™, ์‚ญ์ œ, ๋ชจ๋“  ๋‚ด์šฉ ์ฝ๊ธฐ, ๋ชจ๋“  ํ–‰์„ ์ฝ์–ด์„œ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜ ๋“ฑ
    Files (Java Platform SE 8 )
    https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html

    Path ๊ฐ์ฒด๋ฅผ ์–ป๋Š” ๋ฐฉ๋ฒ•

    Path path1 = Paths.get("save.dat");
    Path path2 = file.thPate();


    ๐Ÿ“์—ฐ์Šต ๋ฌธ์ œ

    6-1

    package com.pkd.Exam;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class Main {
    
        public static void fileCopy(String file) throws IOException {
            int index = file.lastIndexOf(".");
            StringBuilder sb = new StringBuilder();
            sb.append(file.substring(0, index));
            sb.append("(๋ณต์‚ฌ๋ณธ)");
            sb.append(file.substring(index));
            
            FileInputStream fis = new FileInputStream(file);
            FileOutputStream fos = new FileOutputStream(sb.toString());
    
            int i = fis.read();
            while (i != -1) {
                fos.write(i);
                i = fis.read();
            }
            fis.close();
            fos.close();
        }
    
        public static void main(String[] args) {
            try {
                fileCopy("data.txt");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    ๐Ÿ’ก
    fileCopy() ๋ฉ”์„œ๋“œ๋Š” ํŒŒ์ผ์œ„์น˜/ํŒŒ์ผ๋ช…์„ String ํ˜•ํƒœ๋กœ ์ธ์ž๋กœ ๋ฐ›์•„ ๋ณต์‚ฌ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค. data.txtํŒŒ์ผ์„ ์ •์ƒ์ ์œผ๋กœ ๋ณต์‚ฌํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

    6-2

    package com.pkd.Exam;
    
    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.zip.GZIPOutputStream;
    
    public class Main {
        // 6-1
        public static String fileCopy(String fileName) throws IOException {
            String newFileName = "";
    
            int index = fileName.lastIndexOf(".");
            StringBuilder sb = new StringBuilder();
            sb.append(fileName.substring(0, index));
            sb.append("(๋ณต์‚ฌ๋ณธ)");
            sb.append(fileName.substring(index));
    
            newFileName = sb.toString();
    
            FileInputStream fis = new FileInputStream(fileName);
            FileOutputStream fos = new FileOutputStream(newFileName);
    
            int i = fis.read();
            while (i != -1) {
                fos.write(i);
                i = fis.read();
            }
            fis.close();
            fos.close();
            return newFileName;
        }
    
        // ๋ฒ„ํผ๋ง์„ ํ†ตํ•ด ๋ณต์‚ฌ์™€ ์••์ถ•
        public static void fileBufferingGzip(String fileName, String newFileName) throws IOException {
            BufferedReader br = new BufferedReader(new FileReader(fileName));
            BufferedOutputStream bos =
                    new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(newFileName)));
    
            String line = null;
            while ((line = br.readLine()) != null) {
                bos.write(line.getBytes());
                bos.write("\n".getBytes());
            }
    
            br.close();
            bos.close();
        }
    
        public static void main(String[] args) {
            try {
                String fileName = "data.txt";
                fileBufferingGzip(fileName, "data.zip");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    ๐Ÿ’ก
    ํŒŒ์ผ์ด buffering๊ณผ GZIP์„ ํ†ตํ•ด ๋ณต์‚ฌ ๋ฐ ์••์ถ•๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.



    Uploaded by N2T

    728x90
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Keydi's Tistory