常用io

流的分类

  • 从流的方向:输入流、输出流

  • 站在内存的角度,将数据从硬盘读到内存里,输入流。

  • 将数据从内存写到硬盘的文件里,输出流。

  • 从流操作数据时采用的单位:字节流,字符流

以字节为单位处理数据,字节流

以字符为单位处理数据,字符流

字节流字符流
来源流InputStreamReader
接受流OutputStreamWriter

IO结构图

从流的实现方式:处理流(包装流),节点流
假如是直接操作数据,称之为节点流。
假如是在某个节点流或者处理流的基础之间,进行进一步的封装,提供更高级的方法的流,称之为处理流(包装流)

文件字节流

FileInputStream

abstract int read() 从输入流中读取数据的下一个字节。

一次读取一个字节,将读取的数据作为返回值返回,读不到返回-1

FileInputStream构造函数:

alt

使用read()读取数据:

package njlife123;

import java.io.*;
/**
 * 微信公众号:Java后端分享
 * 个人站点:http://www.njlife123.com
 */
public class Test2 {

    public static void main(String[] args) {
        InputStream in = null;
        try {
            File file = new File("test.txt");

            if(file.exists()){
                in = new FileInputStream(file);
                int data = in.read();//读取第一次
                while (data != -1){//判断本地是否读取到数据
                    System.out.println((char)data);
                    data = in.read();//读取下次数据
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

alt

一次将一组数据读入缓冲数组中,返回的是本次读取的字节数
关键代码:

byte buffer [] = new byte[5];
                //读取第一次,得到本次读取的字节长度
                int data = in.read(buffer);
                while (data != -1){//判断本地是否读取到数据
                    for (int i = 0;i < buffer.length;i++){
                        System.out.println((char)buffer[i]);
                    }
                    data = in.read(buffer);//读取下次数据
                }

alt

一次将一组数据读入缓冲数组中,返回的是本次读取的字节数
第2个参数表示将数据放入缓冲数组时,从哪个位置开始存放;
第3个参数表示最多存放多少个字节。
关键代码:

 byte buffer [] = new byte[5];
                //读取第一次,得到本次读取的字节长度
                int data = in.read(buffer,0,buffer.length);
                while (data != -1){//判断本地是否读取到数据
                    for (int i = 0;i < buffer.length;i++){
                        System.out.println((char)buffer[i]);
                    }
                    data = in.read(buffer);//读取下次数据
                }

FileOutputStream

OutputStream:

alt

FileOutputStream:

alt

文件拷贝:

package njlife123;

import java.io.*;

/**
 * 微信公众号:Java后端分享
 * 个人站点:http://www.njlife123.com
 */
public class Test4 {

    public static void main(String[] args) {
        InputStream in = null;
        OutputStream out = null;
        try {
            File sourceFile = new File("test.txt");//源文件
            File destFile = new File("test1.txt");//目标文件
            if(sourceFile.exists()){
                in = new FileInputStream(sourceFile);
                out = new FileOutputStream(destFile);

                byte buffer [] = new byte[5];
                //读取第一次,得到本次读取的字节长度
                int data = in.read(buffer,0,buffer.length);
                while (data != -1){//判断本地是否读取到数据
                    out.write(buffer);
                    data = in.read(buffer);//读取下次数据
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

字符转换流

InputStreamReader

InputStreamReader 是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集。

alt

package njlife123;

import java.io.*;

/**
 * 微信公众号:Java后端分享
 * 个人站点:http://www.njlife123.com
 */
public class Test4 {

    public static void main(String[] args) {
        InputStream in = null;
        InputStreamReader isr = null;
        try {
            File sourceFile = new File("test.txt");//源文件
            if(sourceFile.exists()){
                in = new FileInputStream(sourceFile);
                isr = new InputStreamReader(in);
                char buffer [] = new char[5];
                //读取第一次,得到本次读取的字节长度
                int data = isr.read(buffer,0,buffer.length);
                while (data != -1){//判断本地是否读取到数据
                    System.out.println(buffer[0]);
                    data = isr.read(buffer);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

OutputStreamWriter

alt

package njlife123;

import java.io.*;

/**
 * 微信公众号:Java后端分享
 * 个人站点:http://www.njlife123.com
 */
public class Test4 {

    public static void main(String[] args) {
        OutputStream out = null;
        OutputStreamWriter osw = null;
        try {
            File sourceFile = new File("test.txt");//源文件
            if(sourceFile.exists()){
                out = new FileOutputStream(sourceFile);
                osw = new OutputStreamWriter(out,"utf-8");
                String str = "Hello";
                osw.write(str.toCharArray());
                osw.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
           
        }
    }
}

字符流

FileReader

用来读取字符文件的便捷类。此类的构造方法假定默认字符编码和默认字节缓冲区大小都是适当的。

alt

package njlife123;

import java.io.*;

/**
 * 微信公众号:Java后端分享
 * 个人站点:http://www.njlife123.com
 */
public class Test4 {

    public static void main(String[] args) {
        Reader in = null;
        try {
            File sourceFile = new File("test.txt");//源文件
            if(sourceFile.exists()){
                in = new FileReader(sourceFile);
                char buffer [] = new char[5];
                int data = in.read(buffer,0,buffer.length);
                if(data != -1){
                    for (int i=0;i<buffer.length;i++){
                        System.out.println(buffer[i]);
                    }
                    data = in.read(buffer,0,buffer.length);
                }

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

        }
    }
}

FileWriter

用来写入字符文件的便捷类。此类的构造方法假定默认字符编码和默认字节缓冲区大小都是可接受的。

alt

alt

缓冲流

BufferedReader

特点:从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取
alt

代码:

package njlife123;

import java.io.*;

/**
 * 微信公众号:Java后端分享
 * 个人站点:http://www.njlife123.com
 */
public class Test4 {

    public static void main(String[] args) {
        File file = new File("test.txt");
        InputStream in = null;
        BufferedReader reader = null;
        try {
             in = new FileInputStream(file);
             reader = new BufferedReader(new InputStreamReader(in));
             String data = reader.readLine();//读取第一行
             while (data != null){
                 System.out.println(data);
                 data = reader.readLine();
             }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

BufferedWriter

alt

代码:

package njlife123;

import java.io.*;

/**
 * 微信公众号:Java后端分享
 * 个人站点:http://www.njlife123.com
 */
public class Test4 {

    public static void main(String[] args) {
        File file = new File("test.txt");
        InputStream in = null;
        OutputStream out = null;
        BufferedReader reader = null;
        BufferedWriter writer = null;
        try {
             in = new FileInputStream(file);
             out = new FileOutputStream(new File("qiu.txt"));
             reader = new BufferedReader(new InputStreamReader(in));
             writer = new BufferedWriter(new OutputStreamWriter(out));
             String data = reader.readLine();//读取第一行
             while (data != null){
                 writer.write(data);
                 data = reader.readLine();
             }
            writer.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

字节数组流

特点:读写的目的地是其内置的一个byte数组

ByteArrayInputStream

package com;

import java.io.ByteArrayInputStream;

public class Test2 {

    public static void main(String[] args) {
        String str = "hello";
        byte [] data = str.getBytes();

        ByteArrayInputStream bis = new ByteArrayInputStream(data);
        //可以读取的数据字节数
        System.out.println(bis.available());
        int i = bis.read();

        while (i != -1){
            System.out.print((char)i);
            i = bis.read();
        }
    }
}

ByteArrayOutputStream

当向其写数据时,其内置的byte[]的大小会自动扩充

package com;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

public class Test2 {

    public static void main(String[] args) {
        String str = "hello";
        byte [] data = str.getBytes();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        //将data写入内置的byte[]中
        //ByteArrayOutputStream内置byte数组的大小会会自动扩容以容纳所有数据
        bos.write(data,0,data.length);
        byte[] bytes = bos.toByteArray();
        System.out.println(new String(bytes));
    }
}

备注:还有很多流自己去看吧,在此不一一介绍

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值