본문 바로가기
Programming/Java

[Java]File 읽기(read)/쓰기(write)

by 빅경 2024. 6. 8.
728x90
반응형

 

 

FileInputStream - https://sungwoon.tistory.com/88
FileOutputStream - https://sungwoon.tistory.com/89

import java.io.*;

public class FileExam {
    public static void main(String[] args) throws IOException {
        File f = new File("/Users/test.sh");
        File f2 = new File("/Users/test2.sh");

        System.out.println("f.getAbsolutePath() = " + f.getAbsolutePath());

        if(!f.getParentFile().exists()) {
            f.getParentFile().mkdirs();
        }

        if (!f.exists()) {
            f.createNewFile();
        }

        InputStream is = new FileInputStream(f);
        OutputStream os = new FileOutputStream(f2);
        int data = 0;

		// file read and write
        while((data = is.read()) != -1) {
            char c = (char)data;
            os.write(c);
        }

        is.close();
        os.close();
    }
}
728x90
반응형