IO流

java中io流的分类
1、流向(相较于程序而言)
1)输入流:读数据
2)输出流:写数据
2、数据类型
1)字节流
2)字符流:为了方便操作文本数据,java提供了字符流。
一般来说操作文本数据,就用字符流。否则就用字节流。

一、字节流
字节流有两个抽象基类
1、读取
InputStream

单个字节读取

    public static void main(String[] args) throws Exception {
        FileInputStream fis=new FileInputStream("fos.txt");
        int by=0;
        while((by=fis.read())!=-1) {
            System.out.print((char)by);
        }
        fis.close();
        
    }

注意事项:用字节流如果读取的是中文会有问题,因为中文是多个字节,而这里一个字节就转成char,所以会乱码。

一个字节数组读取

        FileInputStream fis=new FileInputStream("fos.txt");
        int len=0;
        byte[] bytes=new byte[1024];
        while((len=fis.read(bytes))!=-1) {
            System.out.println(new String(bytes,0,len));
        }
        fis.close();

注意事项:fis.read(bytes)返回的不是数组的长度,而是每次读取的字节数。
数组的长度一般是1024,或者1024的整数倍。

2、写入
OutputStream

    public static void main(String[] args) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("fos.txt");
            fos.write("hello IO".getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fos!=null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

注意事项:
1、new FileOutputStream("fos.txt");创建FileOutputStream对象的时候,会自动去创建fos.txt文件
2、记得在finally 中释放资源,且判断不为空。
上面的代码是每次都重头写的,文件以前的数据没有保存。
实现数据的追加:在构造方法的第二个参数,写true即可,其他代码不变。
new FileOutputStream("fos.txt",true);

字节缓存输入流

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("fos.txt"));
        
        //单字节读取
        int by=0;
        while((by=bis.read())!=-1) {
            System.out.print((char)by);
        }
        
        //一个字节数组读取
        byte[] bys=new byte[1024];
        int len=0;
        while((len=bis.read(bys))!=-1) {
            System.out.print(new String(bys,0,len));
        }
        
        bis.close();

字节缓存输出流

        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("fos.txt"));
        bos.write("123".getBytes());
        bos.close();

二、字符流
字符流也有两个抽象基类
1、读取
Reader

        // 指定编码InputStreamReader reader = new InputStreamReader(new FileInputStream("fos.txt"),"UTF-8");
        
        //使用系统默认编码
        InputStreamReader reader = new InputStreamReader(new FileInputStream("fos.txt"));
        
        //一次读取一个字符
//      int ch = 0;
//      while((ch=reader.read())!=-1) {
//          System.out.print((char)ch);
//      }

        //一次读取一个字符数组
        char[] chars = new char[1024];
        int len=0;
        while((len=reader.read(chars))!=-1) {
            System.out.print(new String(chars,0,len));
        }
        
        reader.close();

2、写入
Writer

        //指定编码 OutputStreamWriter out=new OutputStreamWriter(new FileOutputStream("fos.txt"),"utf-8");
        
        //使用系统默认编码
        OutputStreamWriter out=new OutputStreamWriter(new FileOutputStream("fos.txt"));
        out.write("中国");
        out.flush();
        out.close();

注意事项:当写入的数据特别大时,可以写入一段,flush一次。一般而言可以不用写,close关闭前,会自动刷新一次。

由于常见的写法都是使用系统默认编码,而转换流(字符流)的名字过长,所以jdk提供了简化写法。
FileWriter=FileOutputStream+默认编码表(相当于OutputStreamWriter,但是不能指定编码)
FileReader=FileInputStream+默认编码表(相当于InputStreamReader ,但是不能指定编码)

字符流的简化写法:

读取

        FileReader reader=new FileReader("fos.txt");
        // 单个字符读取
//      int ch = 0;
//      while((ch=reader.read())!=-1) {
//          System.out.print((char)ch);
//      }
        
        // 一个字符数组读取
        int len=0;
        char[] chars=new char[1024];
        while((len=reader.read(chars))!=-1) {
            System.out.println(new String(chars,0,len));
        }
        
        reader.close();

写入

        FileWriter writer=new FileWriter("fos.txt");
        writer.write("中国1234");
        writer.close();

字符缓充流
BufferedWriter

        BufferedWriter bw=new BufferedWriter(new FileWriter("fos.txt"));
        bw.write("中国123");
        bw.close();

BufferedReader

        BufferedReader br=new BufferedReader(new FileReader("fos.txt"));
        // 单个字符读取
//      int ch=0;
//      while((ch=br.read())!=-1) {
//          System.out.print((char)ch);
//      }
//      
        // 一个字符数组读取
        char[] chars=new char[1024];
        int len=0;
        while((len=br.read(chars))!=-1) {
            System.out.println(new String(chars,0,len));
        }
        
        br.close();

字符缓存流的特殊方法:
BufferedWriter :newLine()方法,写一行行分隔符。 行分隔符字符串由系统属性line.separator定义,并不一定是单个换行符('\ n')字符。

BufferedReader :readLine()方法,读一行文字。一行被视为由换行符('\ n'),回车符('\ r')中的任何一个或随后的换行符终止。 包含行的内容的字符串,不包括任何行终止字符,如果已达到流的末尾,则为null

NIO:待了解。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。