Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

chapter01 : FileOutput 본문

JAVA/chapter24_io

chapter01 : FileOutput

GAWON 2023. 5. 31. 09:19
package org.joonzis.ex;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Ex01_FileOutput {
	public static void main(String[] args) {

		// 바이트 기반 스트림
		FileOutputStream fos = null;
		BufferedOutputStream bos = null;
		try {
			/*
			 * FileOutputStream(생성할 파일의 경로 및 파일 이름) 
			 * -경로 지정을 안하면 현재 디렉토리에 파일 생성(상대 경로) 
			 * -ex) c:/file/alphabet.txt(절대 경로) 경로의 과정을 전부보여주는것
			 */
			fos = new FileOutputStream("alphabet.txt"); // 파일
			bos = new BufferedOutputStream(fos);

			char ch = 'A'; // 아스키 코드 시작 문자
			while (true) {
				bos.write(ch);
				if (ch == 'z') { // 아스키 코드 종료 문자
					break;
				}
				ch++; // 아스키 코드표 상 다음 문자로 증가
			}
			bos.flush(); // 버퍼 비워주기 (*flush* = 크기와 상관없이 바로 비워준다)중요!!!!
			System.out.println("alphabut.txt 파일 생성!");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 버퍼를 사용한 클래스는finally 에서 close()를 통해 메모리 관리를 효과적으로 할수있다
		}
		try {

			if (bos != null) {
				bos.close();
			}
			if (fos != null) {
				fos.close();
			}
		}catch (Exception e) {
		  e.printStackTrace();
		}

	}
}

'JAVA > chapter24_io' 카테고리의 다른 글

chapter06 : FileCopy  (0) 2023.05.31
chapter05 : FileCopy  (0) 2023.05.31
chapter04 : FileInput  (0) 2023.05.31
chapter03 : FileOutput  (0) 2023.05.31
chapter02 : FileInput  (0) 2023.05.31