IO流,FileInputStream,FileOutputStream,BufferedInputStream,BufferedOutputStream,BufferedReader....

File:file就是指一个文件或文件夹,文件及文件夹的创建、删除需要用到它,可以通过他获取文件的各种信息,但是不能获取文件的内容!!要获取文件的内容需要用到其他的类。

FileInputStream,FileOutputStream 字节流(效率太低)

public static void main(String[] args) {
        FileInputStream fis=null;
        FileOutputStream fos=null;
        try {
            fis=new FileInputStream("F:\\图片\\截图.png");
            fos=new FileOutputStream("F:\\其他\\aa.png");
            byte[] b=new byte[1024*3];
            int length=0;
                try {
                    while (((length=fis.read(b))!=-1)){
                        fos.write(b,0,length);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                fis.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

BufferedInputStream BufferedOutputStream

缓冲字节流是为高效率而设计的,真正的读写操作还是靠FileOutputStreamFileInputStream,所以其构造方法入参是这两个类的对象也就不奇怪了。

public static void main(String[] args) {
        FileInputStream fis=null;
        FileOutputStream fos=null;
        BufferedInputStream bis=null;
        BufferedOutputStream bos=null;
        try {
            fis=new FileInputStream("F:\\图片\\type_back.jpg");
            fos=new FileOutputStream("F:\\其他\\type_back.JPG");
            bis=new BufferedInputStream(fis);
            bos=new BufferedOutputStream(fos);
            byte[] b=new byte[1024*30];
            int length=0;
            try {
                while (((length=bis.read(b))!=-1)){
                    bos.write(b,0,length);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                fis.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

BufferedReader BufferedWriter 简化字符流的读写,new FileWriter等同于new OutputStreamWriter(new FileOutputStream(file, true))

public static void main(String[] args) {
        BufferedReader br=null;
        BufferedWriter bw=null;

        try {
            br=new BufferedReader(new FileReader(new File("f:\\图片\\笔记.txt")));
            bw = new BufferedWriter(new FileWriter(new File("f:\\其他\\1.txt")));
            String temp = "";
            while ((temp = br.readLine()) != null) {
                bw.write(temp);
                bw.newLine();
            }
            bw.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                br.close();
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值