• java中的输入输出<1>


    java中的输入输出基础(1)

    java中的IO支持通过java.io包下的类和接口来支持。在java.io包下主要包括输入、输出两种io流,每种输入、输出流又分为字节流和字符流。

    字节流就是以字节为单位来进行处理输入、输出,字符流就是以字符为单位来进行处理输入、输出的。

    java中的io流还分为底层的节点流和上层的处理流。

    java中的4个抽象基类,注意是抽象类:字节流(InputStream,OutputStream),字符流(Reader,Writer)。

    1、输入流:InputStream/Reader

          InputStream、Reader是所有输入流的基类,他们都是抽象类,所以他们不能创建实例来进行输入。他们有一个用于读取文件的输入流:FileInputStream,FileReader

    //FileInputStream 实例

    FileInputStream fis=new FileInputStream("D:\wang.ini");          //创建字节输入流
    byte[] b=new byte[1024];                                                     //创建一个长度为1024的字节数组
    int length=fis.read(b);                                                           //调用InputStream的read(byte[] b):每次读取b.length字节,返回实际读取的字节数
    if(length>0){                                                                          
    System.out.println(new String(b,0,length));                            //如果有数据,将字节数组转换成字符串输出
    }

    //FileReader实例

    FileReader file=new FileReader("D:\wang.ini");
    char[] c=new char[32];                                                        //字符数组
    int length=file.read(c);                                                   
    if(length>0){
    System.out.println(new String(c,0,length));
    }

    2、OutPutStream/Writer

    输出流  FileOutputStream     FileWriter

      

    FileInputStream fis=null;
    FileOutputStream fos=null;
    try{
    fis=new FileInputStream("D:\wang.ini");
    fos=new FileOutputStream("D:\wang.java");
    byte[] b=new byte[32];
    int length=fis.read(b);
    if(length>0){
    fos.write(b, 0, length);                     //每次以b.length大小输出,从0开始 输出大小为length
    }
    }
    catch(Exception e){
    System.out.println(e.toString());
    }
    finally{
    fis.close();
    fos.close();
    }

    3、通过上面4个抽象基类的用法我们发现,四个基类使用起来有些麻烦,这就需要借助于处理流。处理流,他可以隐藏底层设备上的节点差异,并对外提供方便的输入、输出方法

     //使用PrintStream流来包装OutputStream

    PrintStream ps=null;
    try{
    FileOutputStream fos=new FileOutputStream("D://wang.java");
    ps=new PrintStream(fos);                                    //创建一个PrintStream流来包装FileOutputStream流
    ps.println("你是谁");
    ps.println(new Test());

    }catch(Exception e){
    System.out.println(e.toString());
    }
    finally{
    if(ps!=null){
    ps.close();
    }
    }

    4、一般如果我们进行输入/输出二进制内容,应该考虑使用字节流。如果是文本内容,应该考虑使用字符流。

    5、转换流:InputStreamReader将字节输入流转换成字符输入流、OutputStreamWriter将字节输出流转换成字符输出流。但是没有字符输入流转换成字节输入流和字符输出流转换成字节输出流。文本内容是二进制文件特殊形式。使用字符流更方便。

    //System.in是一个字节输入流,是InputStream流的一个实例,我们可以使用InputStreamReader转换成字符输入流,再将Reader包装成BufferedReader,利用BufferedReader的readline进行一行一行的读入数据。

    InputStreamReader reader=new InputStreamReader(System.in);
    BufferedReader br=new BufferedReader(reader);
    while(br.readLine()!=null){
    if(br.readLine().equals("exit")){
    System.exit(1);
    }
    System.out.println("输出内容为:"+br.readLine());
    }

  • 相关阅读:
    100以内质数的算法
    WebAPI和WebService的区别
    .net core 2.0 数据访问-迁移
    .net core 2.0 Redis的基本使用
    .net core 2.0 Autofac
    net core 2.0 + Autofac的坑
    MVC路由机制
    MVC原理
    CentOS安装GIt、上传项目到git仓库
    ARM 汇编指令集 特点5:ARM 多级指令流水线
  • 原文地址:https://www.cnblogs.com/zailushang117/p/3468375.html
Copyright © 2020-2023  润新知