07-23_01 IO流01

IO的流向说明图解

在这里插入图片描述


IO流的分类

根据数据的流向分为:输入流和输出流。

​ 输入流︰把数据从其他设备上读取到内存中的流。

​ 输出流︰把数据从内存中写出到其他设备上的流。

格局数据的类型分为:字节流和字符流。

​ ● 字节流︰以字节为单位,读写数据的流。

​ ● 字符流︰以字符为单位,读写数据的流。.


字节流

字节输出流(OutputStream

向a.txt里写入数据,(取消了追加模式)

package com.soft.demo01;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class IO_01 {
    public static void main(String[] args) throws Exception {
        //字节输出流,向文件中写入数据
        //1.初始化
        //文件不存在,会自动创建,每一次写覆盖数据
        File file = new File("a.txt");
        //true:追加模式,就是数据都存在,会在上一次数据的后面继续写新的数据
        FileOutputStream fileOutputStream = new FileOutputStream(file,false);

        //2.写数据
        // fileOutputStream.write(97);
        // fileOutputStream.write(98);//ASCII码
        //fileOutputStream.write(99);
        
        //换行\r\n
        //fileOutputStream.write("\r\n".getBytes());
        //fileOutputStream.write("我爱中国".getBytes());

        byte[] buffer = "abcdef".getBytes();

        //abc
        fileOutputStream.write(buffer,0,3);//写入有效的数据的时候会用到   //abc
        
        //3.关闭
        fileOutputStream.close();
    }
}

我爱中国abc

字节输入流(InputStream)

一个字节一个字节的读取a.txt文件

package com.soft.demo01;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class IO_02 {
    public static void main(String[] args) throws Exception {
        //1.读我们的文件
        FileInputStream fileInputStream = new FileInputStream("a.txt");

        //2.一个字节一个字节的读
        int read = fileInputStream.read();
        System.out.println((char)read);
        read = fileInputStream.read();
        System.out.println((char)read);
        read = fileInputStream.read();
        System.out.println((char)read);
        //上面读到了abc
        //读取到文件的结束是-1标志
        read = fileInputStream.read();
        System.out.println(read);
        read = fileInputStream.read();
        System.out.println(read);

        //3.关流
        fileInputStream.close();
    }
}

/**结果为:
a
b
c
*/

循环读取a.txt文件

package com.soft.demo01;

import java.io.FileInputStream;

public class IO_03 {
    public static void main(String[] args) throws Exception {
        //1.读我们的文件
        FileInputStream fileInputStream = new FileInputStream("a.txt");

       //2.使用while进行循环的读取
        int end ;
        while ((end = fileInputStream.read()) != -1) {
            System.out.println((char) end);
        }

        //3.关流
        fileInputStream.close();
    }
}

/**结果为:
a
b
c
*/

复制图片:

package com.soft.demo01;

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

public class IO_05 {
    public static void main(String[] args) throws Exception {
        //1.定义被复制文件对象
        FileInputStream fileInputStream = new FileInputStream("method.png");

        //2.复制到那里
        FileOutputStream fileOutputStream = new FileOutputStream("method_back.png");

        //3.定义我们的缓存的数组,定义读取的长度
        int len;
        byte[] buffer = new byte[1024];

        //4.循环去读取,并写入到新的文件中
        while ((len = fileInputStream.read(buffer)) != -1) {
            //写入到新的文件中
            fileOutputStream.write(buffer, 0, len);//有效数据
        }

        //5.关闭
        fileInputStream.close();
        fileOutputStream.close();
    }
}


字节输入输出结合使用案例:

用户输入一个地址的文件复制到另一个地址里去

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

public class IO_02 {
    public static void main(String[] args) throws Exception{

        //从键盘接收数据
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入数据源地址:");
        //把输入的地址存在path里
        String path = scanner.nextLine();

        //通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例
        File f = new File(path);
        //判断路径是否存在
        boolean exists = f.exists();
        if (exists==true){
            System.out.println("请输入目的地地址:");
        }else {
            System.out.println("路径不存在,请检查输入的路径");
        }

        //从键盘接收数据
        if(scanner.hasNextLine()){
            String path_back = scanner.nextLine();
            readContent(path, path_back);
        }else {
            System.out.println("路径不存在,请检查输入的路径");
        }
        scanner.close();
    }

    private static void readContent(String path, String path_back) throws IOException {
        //指定数据源
        FileInputStream fis = new FileInputStream(path);

        //指定目的地
        FileOutputStream fos3 = new FileOutputStream(path_back);

        // 定义变量,作为有效个数
        int len;
        // 定义字节数组,作为装字节数据的容器
        byte[] b = new byte[1024];
        // 循环读取
        while((len=fis.read(b))!=-1){
            fos3.write(b,0,len);
        }

        //关闭资源
        fis.close();
        fos3.close();
    }

}

请输入数据源地址:
D:\IdeaWorkSpace\Demo\Demo0723\a.txt
请输入目的地地址:
D:\IdeaWorkSpace\Demo\Demo0723\c.txt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值