IO流-基础输入输出

按照操作数据单位不同分为:

字节流:8bit (操作二进制文件,保证文件无损操作

输入(顶层父类):FileInputStream         输出(顶层父类):FileOutputStream

字符流:按照字符为单位(效率高)

输入(顶层父类):Reader                 输出(顶层父类):Writer

按照流的角色分类: 节点流,处理流/包装流

首先针对FileInputStream进行学习说明(写、读):在下面demo中,读取中文出现了乱码问题,在后续中可以解决。

package IO流.FileInputStream;

import java.io.*;

/**
 * @program:多线程和IO
 * @descripton:
 * @author:ZhengCheng
 * @create:2021/10/4-17:44
 **/
public class Demo01 {
    public void outputfile(String filepath){
        File file = new File(filepath);
        FileOutputStream fos = null ;
        try {
           // FileOutputStream fos = new FileOutputStream(file);//这样的写法是覆盖写
            fos = new FileOutputStream(file,true);//这样的写法是续写
            String s = "HelloWorld";
            fos.write(s.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void inputfile(String filepath){
        File file = new File(filepath);
        FileInputStream fis = null;
        int readlen = 0 ;
        try {
            while ((readlen=(fis.read(new byte[1024])))!=-1){//使用一个byte数组,提高效率
                System.out.println((char)readlen);
            }
        }catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

小Demo,复制文件:

package IO流.FileInputStream;

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

/**
 * @program:多线程和IO
 * @descripton:复制一个文件
 * @author:ZhengCheng
 * @create:2021/10/4-18:50
 **/
public class Demo02 {
    public static void main(String[] args) {
        new Demo02().copy("d:\\new1.txt","e:\\nnn.txt");
    }
    public void copy(String fromPath ,String toPath){
        File fromFile = new File(fromPath);
        File toFile = new File(toPath);
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try{
            fos = new FileOutputStream(toFile);
            fis = new FileInputStream(fromFile);
            //read
            int readlen = 0;
            byte[] buffer = new byte[1024];
            while ((readlen = fis.read(buffer)) != -1){
                fos.write(buffer,0,readlen);
            }
            System.out.println("Finish");
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                fis.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

使用Writer和Reader来完成copy。不会中文乱码,因为是按照字符来读的。

package IO流.ReaderWriter;

import IO流.FileInputStream.Demo02;

import java.io.*;

/**
 * @program:多线程和IO
 * @descripton:Copy
 * @author:ZhengCheng
 * @create:2021/10/4-19:24
 **/
public class Demo01 {
    public static void main(String[] args) {
        new Demo01().copy("d:\\new1.txt","e:\\nnn1.txt");
    }
    public void copy(String fromPath ,String toPath){
        File fromFile = new File(fromPath);
        File toFile = new File(toPath);
        FileReader fr = null;
        FileWriter fw = null;
        try{
            fw = new FileWriter(toFile);
            fr = new FileReader(fromFile);
            int readlen = 0 ;
            char[] buffer = new char[1024];
            while ((readlen = fr.read(buffer))!= -1){
                fw.write(buffer,0,readlen);
                fw.flush();//记得flush
            }
            System.out.println("Finish");
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                fr.close();
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

节点流: 从一个特定的数据源读写数据,如FileReader、FileWriter

处理流(包装流):是“连接”在已存在的流(节点流或处理流)之上,为程序提供更为强大的读写功能,如BufferReader(文件/其他数据源)、BufferWriter  -------- 采用了装饰者模式

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值