zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然JAVAIO操作学习笔记:System类对IO的支持和BuffereRead

    import java.io.OutputStream ;
    import java.io.IOException ;
    public class SystemDemo01{
        public static void main(String args[]){
            OutputStream out = System.out ;        // 此时的输出流是向屏幕上输出
            try{
                out.write("hello world!!!".getBytes()) ;    // 向屏幕上输出
            }catch(IOException e){
                e.printStackTrace() ;    // 打印异常
            }
            try{
                out.close() ;    // 关闭输出流
            }catch(IOException e){
                e.printStackTrace() ;
            }
        }
    };
    public class SystemDemo02{
        public static void main(String args[]){
            String str = "hello" ;        // 声明一个非数字的字符串
            try{
                System.out.println(Integer.parseInt(str)) ;    // 转型
            }catch(Exception e){
                System.err.println(e) ;
            }
        }
    };
    public class SystemDemo03{
        public static void main(String args[]){
            String str = "hello" ;        // 声明一个非数字的字符串
            try{
                System.out.println(Integer.parseInt(str)) ;    // 转型
            }catch(Exception e){
                System.out.println(e) ;
            }
        }
    };
    import java.io.InputStream ;
    public class SystemDemo04{
        public static void main(String args[]) throws Exception {    // 所有异常抛出
            InputStream input = System.in ;    // 从键盘接收数据
            byte b[] = new byte[5] ;    // 开辟空间,接收数据
            System.out.print("请输入内容:") ;    // 提示信息
            int len = input.read(b) ;    // 接收数据
            System.out.println("输入的内容为:" + new String(b,0,len)) ;
            input.close() ;    // 关闭输入流
        }
    };
    import java.io.InputStream ;
    public class SystemDemo05{
        public static void main(String args[]) throws Exception {    // 所有异常抛出
            InputStream input = System.in ;    // 从键盘接收数据
            StringBuffer buf = new StringBuffer() ;    // 使用StringBuffer接收数据
            System.out.print("请输入内容:") ;    // 提示信息
            int temp = 0 ;        // 接收内容
            while((temp=input.read())!=-1){
                char c = (char) temp ;    // 将数据变为字符
                if(c=='
    '){    // 退出循环,输入回车表示输入完成
                    break ;
                }
                buf.append(c) ;    // 保存内容
            }
            System.out.println("输入的内容为:" + buf) ;
            input.close() ;    // 关闭输入流
        }
    };
    import java.io.File ;
    import java.io.FileOutputStream ;
    import java.io.PrintStream ;
    public class SystemDemo06{
        public static void main(String args[]) throws Exception {
            System.setOut(
                new PrintStream(
                    new FileOutputStream("d:" + 
                        File.separator + "red.txt"))) ;    // System.out输出重定向
            System.out.print("www.mldnjava.cn") ;    // 输出时,不再向屏幕上输出
            System.out.println(",李兴华") ;
        }
    };
    import java.io.File ;
    import java.io.FileOutputStream ;
    import java.io.PrintStream ;
    public class SystemDemo07{
        public static void main(String args[]){
            String str = "hello" ;        // 声明一个非数字的字符串
            try{
                System.out.println(Integer.parseInt(str)) ;    // 转型
            }catch(Exception e){
                try{
                    System.setOut(
                        new PrintStream(
                            new FileOutputStream("d:"
                                + File.separator + "err.log"))) ;    // 输出重定向
                }catch(Exception e1){
                
                }
                System.out.println(e) ;
            }
        }
    };
    import java.io.ByteArrayOutputStream ;
    import java.io.PrintStream ;
    public class SystemDemo08{
        public static void main(String args[]) throws Exception{    // 所有异常抛出
            ByteArrayOutputStream bos = null ;        // 声明内存输出流
            bos = new ByteArrayOutputStream() ;        // 实例化
            System.setErr(new PrintStream(bos)) ;    // 输出重定向
            System.err.print("www.mldnjava.cn") ;    // 错误输出,不再向屏幕上输出
            System.err.println("李兴华") ;            // 向内存中输出
            System.out.println(bos) ;    // 输出内存中的数据
        }
    };
    import java.io.FileInputStream ;
    import java.io.InputStream ;
    import java.io.File ;
    public class SystemDemo09{
        public static void main(String args[]) throws Exception{    // 所有异常抛出
            System.setIn(new FileInputStream("d:"
                + File.separator + "demo.txt")) ;    // 设置输入重定向
            InputStream input = System.in ;    // 从文件中接收数据
            byte b[] = new byte[1024]    ;// 开辟空间,接收数据
            int len = input.read(b) ;    //接收
            System.out.println("输入的内容为:" + new String(b,0,len)) ;
            input.close() ;    // 关闭输入流
        }
    };
    import java.io.* ;
    public class BufferedReaderDemo01{
        public static void main(String args[]){
            BufferedReader buf = null ;        // 声明对象
            buf = new BufferedReader(new InputStreamReader(System.in)) ;    // 将字节流变为字符流
            String str = null ;    // 接收输入内容
            System.out.print("请输入内容:") ;
            try{
                str = buf.readLine() ;    // 读取一行数据
            }catch(IOException e){
                e.printStackTrace() ;    // 输出信息
            }
            System.out.println("输入的内容为:" + str) ;
        }
    };
  • 相关阅读:
    图片轮播切换
    php用get_meta_tags轻松获取网页的meta信息
    PHP创建桌面快捷方式实例
    php 获取网站根目录的写法
    php mkdir 创建多级目录实例代码
    php计算剩余时间的自定义函数
    php实现获取汉字的首字母实例
    PDO封装函数
    Struts动态表单(DynamicForm)
    [WPF]静态资源(StaticResource)和动态资源(DynamicResource)
  • 原文地址:https://www.cnblogs.com/tszr/p/12161257.html
Copyright © 2011-2022 走看看