zoukankan      html  css  js  c++  java
  • Java IO(五)==>>重点

    打印流:PrintStream与PrintWriter

     

    PrintStream

    该类的定义如下:

      public class PrintStream extends FilterOutputStream implemtns Appendable,Closeable

    可以发现,它是OutputStream的一个子类

    构造方法如下:

      PrintStream(OutputStream out)

    它可以根据实例化的类对象不同,向不同的地方打印,并且在打印流中,提供了更加丰富的打印方法。

    实例

    package com.fuwh.stream;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.PrintStream;
    
    public class PrintStreamTest01 {
        public static void main(String[] args) throws IOException {
            //向文件中打印
            PrintStream ps=new PrintStream(new FileOutputStream(new File("ps.txt")));
            String name="貂蝉";
            int age=18;
            float shengao=1.41f;
            ps.print(name);
            ps.print(age);
            ps.print(shengao);
            ps.close();
        }
    }
    View Code

     这个时候,不知道大家有没有发现,我们一直在用的System.out.println()有啥特点呢,没错,里面也使用到了Println()方法。

    查看System类的定义可以发现,里面存在三个静态字段:

    static PrintStream err:“标准”错误输出流

    static InputStream in:“标准”输入流

    static PrintStream out:“标准” 输出流

     他们都有固定的输入输出端。

     err实例:

    package com.fuwh.system;
    
    import java.io.PrintStream;
    
    public class ErrTest01 {
        public static void main(String[] args) {
            
            PrintStream err=System.err;
            err.print("这是错误信息");
        }
    }
    View Code

     可以看到,用err输出的信息是红色的。

     out实例

    package com.fuwh.system;
    
    import java.io.PrintStream;
    
    public class OutTest01 {
        public static void main(String[] args) {
            
            PrintStream out=System.out;
            out.print("这是输出信息");
        }
    }
    View Code

     这个输出信息就是普通的信息。

     in实例

    package com.fuwh.system;
    
    import java.io.InputStream;
    
    public class InTest01 {
    
        public static void main(String[] args) throws Exception {
            InputStream is=System.in;
            System.out.println("请输入数据:");
            byte[] b=new byte[1024];
            is.read(b);
            System.out.println("输入的内容是:"+new String(b,0,b.length));
            is.close();
        }
    }
    View Code

    在System类中,还存在以下三个方法,可以实现对err,out,in的输入输出重定向

      ·static void setIn(InputStream in)

      ·static void setOut(PrintStream out)

      ·static void setErr(PrintStream err)

     实例--实现对out的输出重定向到文件中

    package com.fuwh.system;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.PrintStream;
    
    public class OutTest02 {
        public static void main(String[] args) throws Exception{
            
            //调用set方法对out的输出重定向到文件中
            System.setOut(new PrintStream(new FileOutputStream(new File("out.txt"))));
            System.out.print("这是输出信息");
        }
    }
    View Code

    缓冲区读取: BufferedReader/BufferedWriter(重点)

    类的定义如下:

      public class BufferedReader extends Reader

      public class BufferedWriter extends Writer

    构造方法:

      BufferedReader(Reader in):创建一个使用默认大小输入缓冲区的缓冲字符输入流

      BufferedReader(Reader in,int size):穿件一个使用指定大小输入缓冲区的缓冲字符流

      

    从字符输入流中读取文件,缓冲各个字符,从何实现字符,数组,和行的高效读取。

    实例1

    package com.fuwh.buffer;
    
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    
    public class BufferedWriterTest01 {
        public static void main(String[] args) throws Exception {
            PrintWriter print=
                new PrintWriter(
                        new BufferedWriter(
                                new OutputStreamWriter(
                                        new FileOutputStream(
                                                new File("buffer.txt")
                                        )
                                )
                        )
                );
            print.write("这个定义好长啊!");
            print.close();
        }
    }
    View Code

     实例2

    package com.fuwh.buffer;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    public class BufferedReaderTest01 {
        public static void main(String[] args) throws Exception{
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.println("请输入内容:");
            String str=br.readLine();
            System.out.println("您输入的内容是:"+str);
            br.close();
        }
    }
    View Code

  • 相关阅读:
    批量给对象属性赋值
    判断linq语句结果是否为空的方法
    linq var出来的object取值问题
    LinQ转换运算符OfType<T>
    C#退出的几种方式
    return的用法
    扩展文字
    Timer的使用方法
    通过16道练习学习Linq和Lambda
    VS2010 无可用源
  • 原文地址:https://www.cnblogs.com/zerotomax/p/6492933.html
Copyright © 2011-2022 走看看