目录
  • io流简介
  • io流原理
  • io 流体系

io流简介

i/o是input/output的缩写, i/o技术是非常实用的技术,用于处理设备之间的数据传输。如读/写文件,网络通讯等。

java程序中,对于数据的输入/输出操作以“流(stream)” 的方式进行。

java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过标准的方法输入或输出数据。

io流原理

输入input:读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中。

输出output:将程序(内存)数据输出到磁盘、光盘等存储设备中。

流的分类

①按操作数据单位不同分为:字节流(8 bit 一般用于非文本文件),字符流(16 bit 一般用于文本文件)

②按数据流的流向不同分为:输入流,输出流(相对的)

③按流的角色的不同分为:节点流(直接处理文件),处理流(处理被包含的流)

io 流体系

java的io流共涉及40多个类,实际上非常规则,都是从如下4个抽象基类派生的。
由这四个类派生出来的子类名称都是以其父类名作为子类名后缀。

节点流和处理流

节点流:直接从数据源或目的地读写数据

处理流:不直接连接到数据源或目的地,而是“连接”在已存在的流(节点流或处理流)之上,通过对数据的处理为程序提供更为强大的读写功能。

节点流操作

读入以filereader为例

import java.io.file;
import java.io.filereader;
import java.io.ioexception;

/**
 * @author: yeman
 * @date: 2021-09-25-16:30
 * @description:
 */
public class filereadertest {
    public static void main(string[] args) {
        filereader filereader = null;
        try { //一定需要try-catch
            //1、实例化file对象,指明要操作的文件
            file file = new file("io\\hello.txt");
            //2、提供具体的流
            filereader = new filereader(file);
            //3、读取操作
            int read = filereader.read(); //空参为一位一位读取,末尾返回-1
            while (read != -1){
                system.out.print((char) read);
                read = filereader.read();
            }
        } catch (ioexception e) {
            e.printstacktrace();
        } finally {
            //4、关闭流
            try {
                if (filereader != null) { //确保不会因具体流未创建而产生空指针异常
                    filereader.close();
                }
            } catch (ioexception e) {
                e.printstacktrace();
            }
        }
    }
}

import java.io.file;
import java.io.filereader;
import java.io.ioexception;

/**
 * @author: yeman
 * @date: 2021-09-25-16:30
 * @description:
 */
public class filereadertest {
    public static void main(string[] args) {
        filereader filereader = null;
        try { //一定需要try-catch
            //1、实例化file对象,指明要操作的文件
            file file = new file("io\\hello.txt");
            //2、提供具体的流
            filereader = new filereader(file);
            //3、读取操作
            char[] chars = new char[5];
            //char型数组为参数,该数组相当于一个容器,把读取放在里面,返回该次读取的个数,末尾返回-1
            // 最后若不够,容器后部分仍为上一次取的,前部分则被新的这次取到的覆盖了
            int length = filereader.read(chars);
            while (length != -1){
                for (int i = 0; i < length; i++) {
                    system.out.print(chars[i]);
                }
                length = filereader.read(chars);
            }
        } catch (ioexception e) {
            e.printstacktrace();
        } finally {
            //4、关闭流
            try {
                if (filereader != null) {
                    filereader.close();
                }
            } catch (ioexception e) {
                e.printstacktrace();
            }
        }
    }
}

写出以filewriter为例

import java.io.*;

/**
 * @author: yeman
 * @date: 2021-09-25-16:30
 * @description:
 */
public class filereadertest {
    public static void main(string[] args) {
        filewriter fw1 = null; //若硬盘中不存在file,创建之;若存在,内容覆盖之
        try {
            //1、实例化file对象,指明要写出的文件
            file file = new file("io\\hi.txt");
            //2、创建具体的流
            fw1 = new filewriter(file);
            //filewriter fw2 = new filewriter(file,false); //若硬盘中不存在file,创建之;若存在,内容覆盖之
            //filewriter fw3 = new filewriter(file,true); //若硬盘中不存在file,创建之;若存在,内容追加之
            //3、写出操作
            fw1.write("hello world!\n",0,5); //写出“hello”
            fw1.write("你好,世界!"); //写出“你好,世界!”
        } catch (ioexception e) {
            e.printstacktrace();
        } finally {
            //4、关闭流
            try {
                if (fw1 != null) fw1.close();
            } catch (ioexception e) {
                e.printstacktrace();
            }
        }
    }
}

实现一个图片复制(读入写出,使用字节流)

import java.io.*;

/**
 * @author: yeman
 * @date: 2021-09-25-16:30
 * @description:
 */
public class filereadertest {
    public static void main(string[] args) {
        fileinputstream fis = null;
        fileoutputstream fos = null;
        try {
            file infile = new file("io\\input.jpg");
            file outfile = new file("io\\output.jpg");

            fis = new fileinputstream(infile);
            fos = new fileoutputstream(outfile);

            byte[] bytes = new byte[1024]; //通常使用1024,2的10次方
            int length = fis.read(bytes);
            while (length != -1){
                fos.write(bytes,0,length);
                length = fis.read(bytes);
            }
        } catch (ioexception e) {
            e.printstacktrace();
        } finally {
            try {
                if (fos != null) fos.close();
            } catch (ioexception e) {
                e.printstacktrace();
            }
            try {
                if (fis != null) fis.close();
            } catch (ioexception e) {
                e.printstacktrace();
            }
        }
    }
}

到此这篇关于java io流之原理分类与节点流文件操作详解的文章就介绍到这了,更多相关java io流内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!