IO5:标准输入输出/转换流--java31(03/01/2016)

package com.atguigu.java;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

import org.junit.Test;

public class TestOtherStream {

    /*
     * 标准的输入输出流:
     * 标准的输出流:System.out
     * 标准的输入流:System.in
     * 
     * 题目:
     * 从键盘输入字符串,要求将读取到的整行字符串转成大写输出,
     * 然后继续进行输入操作,直至当输入“e”或者“exit”时,退出程序。
     */
    @Test
    public void test2(){
        BufferedReader br = null;
        try {
            InputStream is = System.in;//键盘输入的字母是字节流
            InputStreamReader isr = new InputStreamReader(is);//将字节流转换成字符流
            br = new BufferedReader(isr);
            String str;
            while(true){
                System.out.println("请输入字符串:");
                str = br.readLine();
                if(str.equalsIgnoreCase("e") || str.equalsIgnoreCase("exit")){
                    break;
                }
                String str1 = str.toUpperCase();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }               
            }
        }
        
    }
    
    /*
     * 如何实现字节流与字符流之间的转换
     * 转换流:InputStreamReader  OutputStreamWriter
     * 编码:字符串---->字节数组
     * 解码:字节数组---->字符串
     */
    
    @Test
    public void test1(){
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            //解码
            File file = new File("1.txt");
            FileInputStream fis = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(fis, "GBK");
            br = new BufferedReader(isr);
            //编码
            File file1 = new File("2.txt");
            FileOutputStream fos = new FileOutputStream(file1);
            OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
            bw = new BufferedWriter(osw);
            
            String str;
            while((str = br.readLine()) != null){
                bw.write(str);
                bw.newLine();
                bw.flush();
            }
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }               
            }
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }               
            }
            
        }
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容