美文网首页
IO5:标准输入输出/转换流--java31(03/01/201

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

作者: Girl_endless | 来源:发表于2016-03-01 19:16 被阅读14次
    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();
                    }               
                }
                
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:IO5:标准输入输出/转换流--java31(03/01/201

          本文链接:https://www.haomeiwen.com/subject/wgqwkttx.html