首页 > 其他 > 详细

IO流的总结(一)

时间:2018-09-26 23:22:18      阅读:186      评论:0      收藏:0      [点我收藏+]

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();
        //关闭流
    }

}

 

IO流的总结(一)

原文:https://www.cnblogs.com/ahJava/p/9709667.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!