zoukankan      html  css  js  c++  java
  • IO流的总结(一)

    IO流的介绍:

    其实在我们现实生活中有很多流,例如:水流,电流,气流  等等都是是流,但在Java中IO流是指对数据的操作的流。

    按照流的分类:

    1:字节流和字符流

    Reader和InputStream

    2:输入流和输出流。

    InputStream和OutputStream

    字符流的抽象基类:

             * Reader     (读文件)  ,         Writer(写文件)

    由上面四个类派生的子类名称都是以其父类名作为子类的后缀:

                如:FileReader和FileInputStream

    字符流的介绍:

    1.    字符流中的对象融合了编码表一般是GBK  
    2.   字符流相对来说比较适合处理文本数据,不适合处理二进制数据
    3.         字符流以字符为单位,在处理中文时候不会出现乱码

    字符流读写:

    1. 注意事项:

    2. 写入文件后必须要用flush()刷新。

    3. 用完流后记得要关闭流

    4. 使用流对象要抛出IO异常

    5. 定义文件路径时,可以用“/”或者“\”。

    6. 在创建一个文件时,如果目录下有同名文件将被覆盖。

    在读取文件时,必须保证该文件已存在,否则出异常。

     

    字符流写数据实例:FileWriter

    package com.itheima.test;
    
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class Test {
    
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            FileWriter writer = new FileWriter("src/Test2.txt");
           
            // 创建FileWriter对象
            // 写入数据
            
            writer.write("我是字符流");
            writer.flush();
            // 刷新
            System.out.println("写入数据成功");
            if (writer!=null) {
                
                writer.close();
                // 关闭字符流
            }
        }
    
    }

    字符流读数据实例:FileReader

    package com.itheima.test;
    
    import java.io.FileReader;
    import java.io.IOException;
    
    public class Test2 {
    
        @SuppressWarnings("resource")
        public static void main(String[] args) throws IOException {
            //抛出异常
            FileReader reader=null;
            reader=new FileReader("src/Test2.txt");
            //获取FileReader对象
            char[] ch=new char[1024];
            //使用字符数组来存读到的数据
            int count;
            //计算器
            while ((count=reader.read(ch))!=-1) {
                //判断是否还有数据,如果不等于-1那么还有数据
                System.out.println(new String (ch,0,count));
                //打印数据
                
            }
            reader.close();
         //关闭FileReader流 } }

     

    字符流续写数据:FileWriter

    package com.itheima.test;
    
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class Test {
    
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            FileWriter writer = new FileWriter("src/Test2.txt",true);
            //在有参构造函数中追加boolean值,true表示可以在文件末尾追加数据,false表示不能追加数据
            // 创建FileWriter对象
            // 写入数据
            writer.write("我是字符流");
            writer.flush();
            // 刷新
            System.out.println("写入数据成功");
            if (writer!=null) {
                
                writer.close();
                // 关闭字符流
            }
        }
    
    }

    字节流的介绍:

    字节流,主要用来处理字节或二进制对象。

    字节流写文件实例:

    package com.itheima.test;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class FileDemo1 {
    
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            File file=new File("src/demo1.txt");
            //如果有文件就不创建,没有则创建文件
            FileOutputStream out=new FileOutputStream(file);
            //创建FileOutputStream对象
            byte by[]=new byte[1024];
            //创建一个byte类型数组
            String name="刘海清";
            //名字
            by=name.getBytes();
            //把字符串转化为字节数组
            out.write(by, 0, by.length);
            //把字节数组写到文件里,从0到数组的长度
            out.close();
            //关闭FileOutputStream流
        }
    
    }

    字节流读取数据实例:

    package com.itheima.test;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    
    public class FileDemo2 {
    
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            File file=new File("src/demo1.txt");
            FileInputStream in=new FileInputStream(file);
            //输入流
            int count;
            //计数器
            byte[] by=new byte[1024];
            //字节数组
            while((count=in.read(by))!=-1) {
                //如果不等于-1那么还有数据
                System.out.println(new String(by,0,count));
            }
            in.close();
            //关闭流
        }
    
    }

    作业:实现一个文件拷贝的功能

    package com.itheima.test;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class FileDemo2 {
    
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            FileInputStream in=new FileInputStream("src/demo1.txt");
            //输入流
            int count;
            //计数器
            byte[] by=new byte[1024];
            //字节数组
            String name=null;
            while((count=in.read(by))!=-1) {
                //如果不等于-1那么还有数据
                System.out.println(name=new String(by,0,count));
            }
            for (byte c : by) {
                System.err.println(c);
            }
            File file=new File("src/demo2.txt");
            FileOutputStream out=new FileOutputStream(file);
            
            out.write(by, 0, by.length);
            out.close();
            in.close();
            //关闭流
        }
    
    }
    你不会的东西,觉得难的东西,一定不要躲。先搞明白,后精湛,你就比别人优秀了。因为大部分人都不舍得花力气去钻研,自动淘汰,所以你执着的努力,就占了大便宜。奋斗就是每一天都很难,可一年比一年容易。不奋斗就是每一天都很容易,可一年比一年越难。怕吃苦的人吃苦一辈子,不怕吃苦的人吃苦一阵子。拼一个春夏秋冬,赢一个无悔人生
  • 相关阅读:
    iOS:不同属性声明方式的解析
    iOS:图像和点击事件
    iOS:NAV+TABLE结合
    iOS:实现表格填充和选择操作
    iOS: 填充数据表格
    iOS:导航栏的工具条和导航条
    iOS:使用导航栏
    hello,world不使用ARC
    iOS代码实现:创建按钮,绑定按钮事件,读取控件值
    iOS版 hello,world版本2
  • 原文地址:https://www.cnblogs.com/ahJava/p/9709667.html
Copyright © 2011-2022 走看看