关于IO的几种基本操作

package io;


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

public class StreamExample {


    public static void main(String[] args) throws IOException {

        //源文件
        String source = "D:\\example\\stream.txt";
        //目标文件
        String dist = "D:\\example\\streamCopy.txt";
        //开始
        Long stratTime = System.currentTimeMillis();
        //调用方法
        //method1(source,dist);
        //method2(source,dist);
        //method3(source,dist);
        listDir("D:\\example");
        method4(source,dist);
        //结束时间
        Long endTime = System.currentTimeMillis();
        System.out.println("所用时间"+(endTime-stratTime)+"毫秒");
    }

    //基本字节流读一个字节
    public static void method1(String source,String dist) throws IOException {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(source);
            fos = new FileOutputStream(dist);
            int c = 0;
            while((c=fis.read()) != -1) {
                fos.write(c);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (fis != null) {
                fis.close();
            }
            if (fos != null) {
                fos.close();
            }
        }
    }

    //基本字节流读一组字节
    public static void method2(String source,String dist) throws IOException {

        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(source);
            fos = new FileOutputStream(dist);
            //定义字节数组
            byte[] buf = new byte[1024];
            //定义实际读取字节数
            int hasRead = 0;
            while((hasRead = fis.read(buf)) != -1) {
                fos.write(buf, 0, hasRead);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (fis != null) {
                fis.close();
            }
            if (fos != null) {
                fos.close();
            }
        }

    }

    //处理流读取一个字节
    public static void method3(String source,String dist) throws IOException {

        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            fis = new FileInputStream(source);
            //处理流
            bis = new BufferedInputStream(fis);
            fos = new FileOutputStream(dist);
            //处理流
            bos = new BufferedOutputStream(fos);
            int c = 0;
            while((c = bis.read()) != -1) {
                bos.write(c);
            }
            bos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (bis != null) {
                bis.close();
            }
            if (bos != null) {
                bos.close();
            }
        }

    }

    //处理流读取一个数组
    public static void method4(String source,String dist) throws IOException {

        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            fis = new FileInputStream(source);
            //处理流
            bis = new BufferedInputStream(fis);
            fos = new FileOutputStream(dist);
            //处理流
            bos = new BufferedOutputStream(fos);
            int hasRead = 0;
            byte[] buf = new byte[1024];
            while ((hasRead = bis.read(buf)) != -1) {
                bos.write(buf, 0, hasRead);
            }
            bos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (bis != null) {
                bis.close();
            }
            if (bos != null) {
                bos.close();
            }
        }

    }

    //定义读取文件的文件名
    public static void listDir(String dir) throws IOException {

        File file = new File(dir);
        //判断是否为文件目录
        if (!file.isDirectory()) {
            //不是,抛出异常
            throw new IOException(dir + "不是目录");
        }
        //定义文件名数组
        File[] files = file.listFiles();
        for (File f : files) {
            //可能是一个多级目录,使用递归
            if (f.isDirectory()) {
                listDir(f.getAbsolutePath());
            }else {
                System.out.println(f.getAbsolutePath());
            }
        }

    }



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值