• 第二十五节(转换流,打印流)


    转换流主要有两个 InputStreamReader 和 OutputStreamWriter
    
    1. InputStreamReader 主要是将字节流输入流转换成字符输入流
    
    2. OutputStreamWriter 主要是将字节流输出流转换成字符输出流
    
    
    InputStreamReader:
    
    OutputStreamWriter
    
    
    /*
        InputStreamReader 主要是将字节流输入流转换成字符输入流
    
    */
    
    import java.io.*;
    
    public class InputStreamReaderTest01{
        
        public static void main(String[] args){
            
            BufferedReader br = null;
            
            try{
                // 字节输入流
                // FileInputStream fis = new FileInputStream("C:\work\Java\arry.txt");
                // 字符输入流
                // InputStreamReader isr = new InputStreamReader(fis);
                
                br = new BufferedReader(new InputStreamReader(new FileInputStream("C:\work\Java\arry.txt")));
            
                String temp = null;
                while((temp = br.readLine()) != null){
                    System.out.println(temp);
                }
                
                
            } catch(FileNotFoundException e){
                e.printStackTrace();
            } catch(IOException e){
                e.printStackTrace();
            } finally{
                try{
                    if(br != null){
                        br.close();
                    }
                } catch(IOException e){
                    e.printStackTrace();    
                }
            }
            
            
        }
    
    }
    ////////////////////////////////////////////////////////////////
    
    
    /*
        OutputStreamWriter 主要是将字节流输出流转换成字符输出流
    */
    
    import java.io.*;
    
    public class OutputStreamWriterTest01{
        
        public static void main(String[] args){
            
            BufferedWriter bw = null;
            
            try{
                
                // 字节输出流
                //FileOutputStream fos = new FileOutputStream("C:\work\Java\arry.txt");
                // 字符输出流
                //OutputStreamWriter osw = new OutputStreamWriter(fos);
                
                bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:\work\Java\arry.txt")));
                
                bw.write("中山学院");
                bw.newLine();
                
                bw.write("我爱我的祖国");
            
                bw.flush();
            
            } catch(FileNotFoundException e){
                e.printStackTrace();    
            } catch(IOException e){
                e.printStackTrace();    
            } finally{
                try{
                    if(bw != null){
                        bw.close();    
                    }
                } catch(IOException e){
                    e.printStackTrace();
                }
            }
        }
    
    }
      
    打印流主要包含两个:PrintStream 和 PrintWriter,分别对应字节流和字符流
    
    
    完成屏幕打印的重定向
    
    
    
    用法同 StringBuffer, StringBuilder 和 StringBuffer 的区别是 StringBuffer 中所有的方法都是同
    
    步的,是线程安全的,但速度慢,StringBuilder 的速度快,但不是线程安全的
    
    
    
    
    /*
    完成屏幕打印的重定向
    
        System.out 对应的就是 PrintStream , 默认在输出在控制台,我们可以重定向他的输出,
        可以定向到文件 就是 执行 System.out.println("ming");  不输出到屏幕,而输出到文件    
    
    */
    
    import java.io.*;
    
    public class PrintStreamTest01{
        
        public static void main(String[] args){
            
            FileOutputStream fos = null;
            
            try{
            
                fos = new FileOutputStream("C:\work\Java\arry.txt");
                
                PrintStream ps = new PrintStream(fos);
                
                System.setOut(ps);
                
                System.out.println("ming很帅 !");
                
            } catch(FileNotFoundException e){
                e.printStackTrace();    
            } catch(IOException e){
                e.printStackTrace();        
            } finally{
                try{
                    if(fos != null){
                        fos.close();
                    }
                }catch(IOException e){
                    e.printStackTrace();    
                }
            }
        }
    
    }
    
    ///////////////////////////////////////////////////////////////////
    
    /*
        System.in 可以接收屏幕的输入
    
    */
    
    import java.io.*;
    
    public class PrintStreamTest02{
        
        public static void main(String[] args){
            
            BufferedReader br = null;
            
            try{
            
                InputStreamReader isr = new InputStreamReader(System.in);
                
                br = new BufferedReader(isr);
                
                String temp = null;
                while((temp = br.readLine()) != null){
                    System.out.println(temp);
                    // 如果输出 w 退出循环
                    if("w".equals(temp)){
                        break;    
                    }    
                }
            } catch(FileNotFoundException e){
                e.printStackTrace();    
            } catch(IOException e){
                e.printStackTrace();
            } finally{
                try{
                    if(br != null){
                        br.close();    
                    }
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        }
    
    }
  • 相关阅读:
    《Cracking the Coding Interview》——第6章:智力题——题目2
    《Cracking the Coding Interview》——第6章:智力题——题目1
    《Cracking the Coding Interview》——第5章:位操作——题目8
    《Cracking the Coding Interview》——第5章:位操作——题目7
    《Cracking the Coding Interview》——第5章:位操作——题目6
    Spyder 调出绘图界面
    作为非计算机专业的学生,觉得 C 语言远比其他语言易于上手,正常吗?
    vs2015 + Python3.5 环境搭建
    更新32位Spyder从3.0.0-> 3.2.3
    luogu P1047 校门外的树 x
  • 原文地址:https://www.cnblogs.com/Deng1185246160/p/4234314.html
Copyright © 2020-2023  润新知