본문 바로가기

카테고리 없음

java file io

package test.function;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

public class FileWrite {

    public static void main(String[] args) throws Exception {
        new FileWrite().fielWrite();
    }    // end main()

    
    /******************************************
     * 파일을 생성해서 내용 쓰기
     * 
     * @author     younguk.jeong
     * @since    2008/09/10
     * @throws     Exception
     ******************************************/
    public void fielWrite() throws Exception {
        /* 파일을 생성해서 내용 쓰기 */
        String szFileName = "fileWrite.txt";                    // 파일 이름
        File file = new File(szFileName);                        // 파일 생성
        OutputStream out = new FileOutputStream(file);            // 파일에 문자를 적을 스트림 생성
        
        String szContent = "";
        szContent += "Motivation is what gets you started, habit is what keeps you going. - Jim Ryun \n";
        szContent += "*동기가 시작하게 만드는 힘이라면, 습관은 지속할 수 있도록 하는 힘이다.";
        
        out.write(szContent.getBytes());                        // 파일에 쓰기
        out.close();                                            // 파일 쓰기 스트림 닫기
        
        
        /* 생성된 파일 내용 확인 */
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec("cat " + file.getAbsolutePath());
        
        System.out.println("#### 파일 내용 확인 ####");
        System.out.println("cat " + file.getAbsolutePath());
        
        String szLine = "";
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
        while ((szLine = br.readLine()) != null)
             System.out.println(szLine);

        
        /* 생성한 파일 삭제 */
        file.deleteOnExit();    
    }    // end function fileWrite()
}    // end class



깔끔하네요 ㅋ