IO流(续)

IO流(续)

输入流与输出流

  • 把电脑硬盘上的数据读到程序中,称为输入,即input,进行数据的read操作

  • 从程序往外部设备写数据,称为输出,即output,进行数据的write操作

传输方向

​ 输入流:往程序中读叫输入流。

​ 输出流:从程序中往外写叫输出流。

所有输入流都是InputStream类或者Reader类的子类。

​ 类名以InputStream结尾的类都是InputStream的子类。

​ 类名以Reader结尾的类都是Reader类的子类。

所有输出流都是OutputStream类或者Writer类的子类。

​ 类名以OutputStream结尾的类都是OutputStream的子类。

​ 类名以Writer结尾的类都是Writer类的子类。

扫描二维码关注公众号,回复: 12215150 查看本文章

字节流与字符流

从数据流编码格式上划分为

  • ​ 字节流

  • ​ 字符流

InputStream和OutputStream的子类都是字节流

可以读写二进制文件,主要处理音频、图片、歌曲、字节流,处理单元为1个字节。

字节流中常用类

​ 字节输入流 FileInputStream

​ 字节输出流 FileOutputStream

Reader和Writer的子类都是字符流

  • ​ 主要处理字符或字符串,字符流处理单元为2个字节。

  • ​ 字节流将读取到的字节数据,去指定的编码表中获取对应文字。

字符流中常用类

​ 字符输入流 FileReader

​ 字符输出流 FileWriter

package day2;

import java.io.*;

public class StreamDemo {
    
    
    public static void main(String[] args) throws IOException {
    
    
        File file=new File("D:\\789.txt");
        FileInputStream in=null;
        FileOutputStream out=null;
        try {
    
    
            //文件输入字节流(输入管道)
           in=new FileInputStream(file);
            int b=0;
            //指定输出文件地址,如果文件不存在,就会自动创建
             out=new FileOutputStream("D:\\jei.txt");

            while ((b=in.read())!=-1){
    
    
                //System.out.println(b);
                out.write(b);
            }
        } catch (FileNotFoundException e) {
    
    
            e.printStackTrace();
            System.out.println("路径不正确");
        } catch (IOException e) {
    
    
            e.printStackTrace();
            System.out.println("读写终止");
        }finally {
    
    
            in.close();//即使不出异常,也要关闭读写过程
            out.close();
        }
    }
}

package day2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class StreamDemo2 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        File f=new File("D:\\常用工具包\\取色.exe");
        f.createNewFile();
        //输入流 向程序中读入数据
        FileInputStream in=new FileInputStream(f);
        //输出流  从程序向外输出数据
        FileOutputStream out=new FileOutputStream("D:\\常用工具包\\色.exe");

        byte[] b=new byte[1024];
        int length = 0;
        while ((length=in.read(b))!=-1){
    
    
            System.out.println(length);
            out.write(b,0,length);
        }
        in.close();
        out.close();
    }
}

package day2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class StreamDemo3 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //输入流 向程序中读入数据
        FileInputStream in = new FileInputStream("D:\\feiQ.exe");
        //输出流  从程序向外输出数据
        FileOutputStream out = new FileOutputStream("F:\\feiQ12.exe");
            /*
              FileInputStream  int read();  每次从输入流中读取一个字节  返回编码  文件读完返回-1
                               int read(byte[] b) 每次从输入流中读一个byte数组个字节,
                                                  返回的是数组中实际装入的字节数量,文件读完返回-1


                FileOutputStream  write(int b) 一次向外写一个字节
                                  write(b,0,length);
                                   一个向外输出一个byte数组,从指定的位置开始,写length个长度
             */
        byte[] b = new byte[1024];
        int length=0;
        while((length=in.read(b))!=-1){
    
    
            System.out.println(length);
            out.write(b,0,length);
        }

        in.close();
        out.close();

    }
}

根据封装类型不同流又分为

​ 节点流

​ 处理流

节点流:

​ 如果流封装的是某种特定的数据源,如文件、字符串、字符串数组等,则称为节点流。

处理流。

​ 如果流封装的是其它流对象,称为处理流。

​ 处理流提供了缓冲功能,提高读写效率,同时增加了一些新的方法。

节点流中常用类

​ 字节输入流 FileInputStream

​ 字节输出流 FileOutputStream

​ 字符输入流 FileReader

​ 字符输出流 FileWriter

处理流中常用类

​ 缓冲字节输出流 BufferedOutputStream

​ 缓冲字节输入流 BufferedInputStream

​ 缓冲字符输入流 BufferedReader

​ 缓冲字符输出流 BufferedWriter

package day2;

import java.io.*;

public class StreamDemo4 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        /*
        节点流:直接包含的是文件,字符串等数据
        处理流:包含的是其他流对象
        BufferedInputStream,  BufferedoutputStream   内部提供一个缓冲数组
         */

        File f=new File("D:\\常用工具包\\取色.exe");
        FileInputStream bin=new FileInputStream(f);
        //输入流 向程序中读入数据   节点流  包含的是文件,字符串等数据
        BufferedInputStream bf=new BufferedInputStream(bin);
        //输出流  从程序向外输出数据
        FileOutputStream out=new FileOutputStream("D:\\常用工具包\\色.exe");
        BufferedOutputStream bout=new BufferedOutputStream(out);
        byte[] b=new byte[1024];
        int length=0;
        while ((length=bf.read(b))!=-1){
    
    
            bout.write(b,0,length);
        }
        bf.close();
        bout.flush();//刷新缓冲输出流
        bout.close();
    }
}

输入输出节点字符流

Reader 的基本方法

读取一个字符并以整数的形式返回,

如果返回-1已到输入流的末尾。

int read() throws IOException

读取一系列字符并存储到一个数组buffer,

返回实际读取的字符数,如果读取前已到输入流的末尾返回-1

int read(char[] cbuf) throws IOException

关闭

void close() throws IOException

Writer 的基本方法

向输出流中写入一个字符数据,该字节数据为参数b的16位

void write(int c) throws IOException

一个字符类型的数组中的数据写入输出流,

void write(char[] cbuf) throws IOException

将一个字符类型的数组中的从指定位置(offset)开始的

length个字符写入到输出流

void write(char[] cbuf, int offset, int length) throws IOException

注意:
字符流只能读纯文本

package day2;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ReaderWriteDemo {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //字符流,每次直接读到字符,底层有一个转换流
        //字符流只能读纯文本文件
        FileReader reader=new FileReader("D:\\jei.txt");
        FileWriter writer=new FileWriter("D:\\jei1.txt");
        /*System.out.println(reader.read());
        System.out.println(reader.read());
        System.out.println(reader.read());
        System.out.println(reader.read());
        System.out.println(reader.read());*/
    int c=0;
        while ((c=reader.read())!=-1){
    
    
            writer.write(c);
        }
        reader.close();
        writer.flush();
        writer.close();
    }
}

package day2;

import java.io.*;

public class ReaderWriteDemo3 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //字符流,每次直接读到字符,底层有一个转换流
        //字符流只能读纯文本文件
        FileReader reader=new FileReader("D:\\jei.txt");
        BufferedReader br=new BufferedReader(reader);
                                        //向文件中追加内容
        FileWriter writer=new FileWriter("D:\\jei1.txt",true);
        BufferedWriter bw=new BufferedWriter(writer);
        String str=null;
        while ((str=br.readLine())!=null){
    
    
            System.out.println(str);
            bw.write(str);
            bw.newLine();//追加内容时换行
        }
        br.close();
        bw.flush();
        bw.close();
    }
}

猜你喜欢

转载自blog.csdn.net/ZJ_1011487813/article/details/111034159