javaIO 흐름 연습 2: 콘 솔 에서 내용 을 입력 하고 입력 한 내용 을 XX 디스크 의 한 폴 더 의 한 파일 에 기록 합 니 다.

6966 단어 자바
package yuecheng.ch18;

import java.io.*;

public class BufferedInpuStreamDemo
{
/**
 *         ,        E              
 */
public static void main(String[] args)
{
    //systemStreamMethodDemo();
    try
    {
        systemToFileDemo();
    } catch (Exception e)
    {
        e.printStackTrace();
    }
}

/**
 *                 
 */
private static void systemToFileDemo() throws IOException
{
    BufferedWriter  bw= null;
    try
    {
        bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("E:/java.txt")));
    } catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    String line=null;
    while((line=br.readLine())!=null){
        if("over".equals(line)){
            break;
        }
        try
        {
            bw.write(line);
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        try
        {
            bw.newLine();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        try
        {
            bw.flush();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    try
    {
        br.close();
    } catch (IOException e)
    {
        e.printStackTrace();
    }
    try
    {
        bw.close();
    } catch (IOException e)
    {
        e.printStackTrace();
    }
}

/**
 *         
 */
private static void systemStreamMethodDemo() throws IOException
{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
    String line=null;
    while((line=br.readLine())!=null){
        if("over".equals(line)){
            break;
        }
        try
        {
            bw.write(line.toUpperCase());
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        try
        {
            bw.newLine();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        try
        {
            bw.flush();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    /**
     *           :    ,    。
     */
    try
    {
        br.close();
    } catch (IOException e)
    {
        e.printStackTrace();
    }
    try
    {
        bw.close();
    } catch (IOException e)
    {
        e.printStackTrace();
    }
}


}

좋은 웹페이지 즐겨찾기