zoukankan      html  css  js  c++  java
  • Java——I/O入门相关练习代码


    流的概念

    • 数据流向某个对象的数据序列,并且到达这个对象的过程。

    • 输入流:数据源数据向计算机内存的过程。

    • 输出流:把数据从程序流向目标数据源的过程。

    • 字节流:以字节为数据单位来处理的流。

    • 字符流:以字符为数据单位来处理的流。

    流的父类 :

    输入流:InputStream(字节输入流)和Reader(字符输入流)为基类
    输出流:OutputStream(字节输出流)和Writer(字符输出流)为基类


    读取文件

    读取文件1

    //磁盘路径两种表示方式:
            //  1: \   2: /
            try {
                //从文件地址中读取内容到程序中
                InputStream is = new FileInputStream("D:/IOFile/Ch02.txt");
                //开始读取信息
                //先定义一个字节数组存放数据
                byte[] b = new byte[5];
                //声明一个int存储每次读取到的数据
                int i=0;
                //定义一个 记录索引的变量
                int index = 0;
                //循环读取每个数据
                while((i=is.read())!=-1){
                    b[index]=(byte)i;
                    index++;
                }
                //如何把字节数组转成字符串
                System.out.println(new String(b));
                //关闭流
                is.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                //文件没有找到异常
                e.printStackTrace();
            } catch (IOException e) {
                //文件读写异常
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    读取文件2

    //磁盘路径两种表示方式:
            //  1: \   2: /
            try {
                //从文件地址中读取内容到程序中
                InputStream is = new FileInputStream("D:/IOFile/Ch02.txt");
                //开始读取信息
                //先定义一个字节数组存放数据
                byte[] b = new byte[5];
                //完整的读取一个文件
                is.read(b);
                //read返回读取的文件大小
                //最大不超过b.length,实际读取以文件大小为准
                //打印的字节
                System.out.println(Arrays.toString(b));
                //如何把字节数组转成字符串
                System.out.println(new String(b));
    //          while(is.read(b)!=-1){
    //              
    //          }
                //关闭流
                is.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                //文件没有找到异常
                e.printStackTrace();
            } catch (IOException e) {
                //文件读写异常
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    读取文件3

    public static void main(String[] args){
            //磁盘路径两种表示方式:
            //  1: \   2: /
            try {
                //从文件地址中读取内容到程序中
                InputStream is = new FileInputStream("D:/IOFile/Ch02.txt");
                //开始读取信息
                //先定义一个字节数组存放数据
                byte[] b = new byte[8];
                //完整的读取一个文件
                int off=0;
                byte [] c=new byte[is.available()]; //返回文件的大小
                while(is.read(b,off,2)!=-1){
                    off+=2; 
    //              System.out.println(off);
                }
    //          is.read(b,0,2);
    //          is.read(b,off,len);
                //read返回读取的文件大小
                //最大不超过b.length,实际读取以文件大小为准
                //打印的字节
                System.out.println(Arrays.toString(b));
                //如何把字节数组转成字符串
                System.out.println(new String(b));
    //          while(is.read(b)!=-1){
    //              
    //          }
    
                //关闭流
                is.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                //文件没有找到异常
                e.printStackTrace();
            } catch (IOException e) {
                //文件读写异常
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    读取文件4

    public static void main(String[] args) {
    
            try {
                FileInputStream fis = new FileInputStream("D:/IOFile/Ch05.txt");
                BufferedInputStream bis = new BufferedInputStream(fis);
                //读取文件内容
                byte [] b= new byte[bis.available()];
                bis.read(b);
                System.out.println(new String(b));
                // String(byte[])把字节数组转成字符串
    
    
    //  错误:不要尝试 
    //          char [] c = new char[b.length];
    //          for(int i=0;i<c.length;i++){
    //              c[i]=(char)b[i];
    //          }
    //          System.out.println(Arrays.toString(c)); //乱码,一个字节表示不了汉字
    
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    skip:跳过n个字节后再开始读取

    //读取文件
                FileInputStream file = new FileInputStream("D:/IOFile/Ch02.txt");
                byte[] b = new byte[file.available()];
                //skip:跳过n个字节后再开始读取
                file.skip(2);
                file.read(b);
                System.out.println(new String(b));
                file.close();

    读取过程中暂停,给当前位置做一个标记,下一次从标记位置开始读取

    try {
                //FileInputStream 必须要传一个文件名
                BufferedInputStream bis=new BufferedInputStream(new FileInputStream("D:/IOfile/ch07.txt"));
                byte[] b=new byte[bis.available()];
                //设置断点
                bis.mark(bis.read(b,0,b.length/2));
                System.out.println(new String(b));
                System.out.println("暂停读取...");
                Thread.sleep(2000);
                System.out.println("继续读取...");
                //reset将当前复位的位置设置成上次调用mark标记的位置
                bis.reset();
                bis.read(b,b.length/2,b.length/2);
                System.out.println(new String(b));
    //          bis.reset();
    //          bis.read(b);
    //          System.out.println(new String(b));
                bis.close();
            }

    序列流(集合流)

    把n个流合并在一起读取。

    try {
                //第一个文件流
                FileInputStream fis1=new FileInputStream("D:/IoFile/ch02.txt");
                //第二个文件流
                FileInputStream fis2=new FileInputStream("D:/IoFile/ch03.txt");
                //合并到序列流中
                SequenceInputStream sis=new SequenceInputStream(fis1, fis2);
                //方式1
    //          //临时存放数据的数组
    //          int len =fis1.available()+fis2.available();
    //          byte[] b=new byte[2*len+1];
    //          //把每一次读取到的临时数据存放如sb中
    ////            StringBuffer sb=new StringBuffer();
    //          //一次性读取所有的内容
    //          int off=0;
    //          int i=0;
    //          while((i=sis.read(b,off,len))!=-1) { 
    ////                sb.append();
    //              off+=i;
    //          }
    //          System.out.println(new String(b));
    
                //方式2
                byte[] b=new byte[fis1.available()];
    //          StringBuffer sb=new StringBuffer();
    //          int i=0;
                while(sis.read(b)!=-1) {
                    System.out.println(new String(b));
    //              sb.append(new String(b));
                }
    //          System.out.println(sb.toString());
                sis.close();
            }

    把三个流添加到集合中,合并在一个序列流中

    try {
                //三个文件流
                FileInputStream fis1 = new FileInputStream("D:/IoFile/Ch09.txt");
                FileInputStream fis2 = new FileInputStream("D:/IoFile/Ch0901.txt");
                FileInputStream fis3 = new FileInputStream("D:/IoFile/Ch0902.txt");
                //把三个流添加到集合中
                Vector<FileInputStream> vector= new Vector<>();
                vector.add(fis1);
                vector.add(fis2);
                vector.add(fis3);
    
    //          vector.elements();
                //合并在一个序列流中
                SequenceInputStream sis = new SequenceInputStream(vector.elements());
                byte[] b = new byte[fis1.available()+fis2.available()+fis3.available()];
                int off=0;
                for(int i=0;i<vector.size();i++){
                    //off是数组当中存放数据的起始下标位置
                    off+=sis.read(b,off,vector.get(i).available());
                }
                System.out.println(new String(b));
                sis.close();
    
            }
  • 相关阅读:
    Blink示例程序
    arduino入门笔记
    Opentrains 1519 G——最小圆覆盖
    最小圆覆盖入门
    最小球覆盖——模拟退火&&三分套三分套三分
    DAO,Service,Controller各层之间的关系
    史上最全的SpringMVC学习笔记
    pom.xml
    Tomcat入门指南
    tomcat架构
  • 原文地址:https://www.cnblogs.com/aixing/p/13327726.html
Copyright © 2011-2022 走看看