Java基础(25)IO流概述、字节流

1. IO流

1. IO流的概述

(1)IO流是用来处理设备之间的数据传输
(2)Java对数据操作是通过的方式传输的
(3)Java中用于操作流的对象都位于java.io包下

2. IO流的分类

(1)按照数据的流向(站在内存的角度)

  • 输入流:读取数据
  • 输出流:写入数据

(2)按照数据类型

  • 字节流:可以读写任意类型的文件(音频,视频,文本文件)
  • 字符流:只能读写文本文件
  • 一般用字节流的居多

3. IO流基类的概述

(1)字节流的抽象基类:InputStream、OutputStream
(2)字符流的抽象基类:Reader、Writer

2. 字节流

1. 字节流的继承关系
在这里插入图片描述
2. FileOutputStream:文件输出流,用于将数据写入File

(1)构造方法

	FileOutputStream(File file):创建一个向指定 File 对象表示的文件中写入数据的文件输出流
	FileOutputStream(String name):创建一个向具有指定名称的文件中写入数据的文件输出流
public class TestDemo01 {
    public static void main(String[] args) throws IOException {
        //FileOutputStream(File file):创建一个向指定 File 对象表示的文件中写入数据的文件输出流
        File file1 = new File("lol.txt");
        /**
         * 创建一个字节输出流来关联一个文件
         * 当关联的文件不存在时,JVM会自动帮你创建这个文件
         */
        FileOutputStream fos1 = new FileOutputStream(file1);

        //FileOutputStream(String name):创建一个向具有指定名称的文件中写入数据的文件输出流
        FileOutputStream fos2 = new FileOutputStream("haha.txt");
		
		//记得要关闭这个流
		fos1.close();
		fos2.close();
    }
}

(2)在创建字节输出流对象时做了那几件事情

①调用系统资源创建了lol.txt文件
②创建了fos1对象
③把fos1对象指向这个文件

(3)FileOutputStream的三个write()方法

	public void write(int b):写一个字节  超过一个字节 砍掉前面的字节
	public void write(byte[] b):写一个字节数组
	public void write(byte[] b,int off,int len):写一个字节数组的一部分

	windows下的换行符: \r\n
public class TestDemo02 {
    public static void main(String[] args) throws IOException {
        //public void write(int b):写一个字节
        //超过一个字节 砍掉前面的字节
        FileOutputStream fos = new FileOutputStream("haha.txt");
        fos.write(97);
        fos.write(98);
        fos.write(99);
        fos.write(100);

        //public void write(byte[] b):写一个字节数组
        byte[] bytes = {101,102,103,104};
        fos.write(bytes);

        //public void write(byte[] b,int off,int len):写一个字节数组的一部分
        String str = "英雄联盟";
        byte[] strBytes = str.getBytes();
        fos.write(strBytes,0,6);

		//释放资源
		fos.close();
    }
}

//结果如下
//abcdefgh英雄

(4)FileOutputStream中可以追加写入的文件输出流的构造方法

	FileOutputStream(File file, boolean append):创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
    FileOutputStream(String name, boolean append):创建一个向具有指定 name 的文件中写入数据的输出文件流。
    是否要追加写入,true 追加写入。false 不追加
    默认是false,不追加
public class TestDemo03 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("lol.txt", true);
        fos.write("英雄联盟".getBytes());
        fos.write("\r\n".getBytes());
        fos.write("影流".getBytes());
    }
}

//将这个程序运行了3次,结果如下
/**
英雄联盟
影流
英雄联盟
影流
英雄联盟
影流
*/

3. FileInputStream:从文件系统中的某个文件中获得输入字节
(1)构造方法

	FileInputStream(File file):通过打开一个到实际文件的连接来创建一个FileInputStream,该文件通过文件系统中的 File 对象 file 指定
	FileInputStream(String name):通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定
public class TestDemo01 {
    public static void main(String[] args) throws FileNotFoundException {
    	//【注意】字节输入流如果所关联的文件不存在,则会报错
        
        //FileInputStream(File file):
        //通过打开一个到实际文件的连接来创建一个FileInputStream,该文件通过文件系统中的 File 对象 file 指定
        File file = new File("haha.txt");
        FileInputStream fis1 = new FileInputStream(file);

        //FileInputStream(String name)
        //通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定
        FileInputStream fis2 = new FileInputStream("haha.txt");
    }
}

(2)FileInputStream的三种read()方法

	 int read() :从此输入流中读取一个数据字节 
	 int read(byte[] b) :从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中 
	 int read(byte[] b, int off, int len) : 从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。
	 如果读取不到有效的字节,就会返回-1 ,我们经常使用-1来判断这个文件的数据是否读取完毕
//haha.txt: abcdefgh英雄

public class TestDemo02 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("haha.txt");

        //int read() :从此输入流中读取一个数据字节
        int by = fis.read();
        System.out.println(by);  //97

        by = fis.read();
        System.out.println(by);   //98

        by = fis.read();
        System.out.println(by);   //99

        //如果读取不到有效的字节,就会返回-1 ,我们经常使用-1来判断这个文件的数据是否读取完毕

        fis.close();
    }
}

========================================================================

//haha.txt: abcdefgh英雄

public class TestDemo03 {
    public static void main(String[] args) throws IOException {
        //int read(byte[] b) :从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中
        FileInputStream fis = new FileInputStream("haha.txt");

        byte[] bytes = new byte[1024];

        //把读取到的字节,传入这个数组中
        int len = fis.read(bytes);
        System.out.println("读取到的字节数为:" + len);   //读取到的字节数为:14

        System.out.println(Arrays.toString(bytes));   //[97, 98, 99, 100, 101, 102, 103, 104, -24, -117, -79, -23, -101, -124, 0, 0, 0, 0, 0, 0, 0, 0, 0...]

        String str = new String(bytes);
        System.out.println(str);   //abcdefgh英雄
    }
}

4. FileOutputStream + FileInputStream:对文件进行复制粘贴
(1)一次读写一个字节来复制(效率很低)

public class TestDemo01 {
    public static void main(String[] args) throws IOException {
        //向E盘中复制haha.txt文件
        FileInputStream fis = new FileInputStream("haha.txt");
        FileOutputStream fos = new FileOutputStream("E:\\haha1.txt");

        int by = 0;
        while ((by = fis.read()) != -1 ){
            fos.write(by);
            fos.flush();   //刷新
        }

        fis.close();
        fos.close();
    }
}

(2)一次读写一个字节数组来复制(效率高)【模板】

public class TestDemo02 {
    public static void main(String[] args) throws IOException {
        //向E盘中复制haha.txt文件
        FileInputStream fis = new FileInputStream("haha.txt");
        FileOutputStream fos = new FileOutputStream("E:\\haha2.txt");

        //定义一个字节数组,来充当缓冲区
        byte[] bytes = new byte[1024 * 8];
        
        //定义一个变量,记录每次读取到的有效字节数
        int len = 0;

        while ((len = fis.read(bytes)) != -1){
            fos.write(bytes,0,len);
            fos.flush();
        }

        fis.close();
        fos.close();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值