四种节点流和Buffered的使用

1、FileInputStream的使用

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


public class TestFileInputOutputStream {
    /**
     * 从硬盘中读取一个文件内容到程序中
     * */

    public static void main(String[] args) {
        String counte = "河南理工大学";

//      testFileInputStream1();
//      testFileInputStream2();
//      testFileInputStream3(counte);
        long start = System.currentTimeMillis();
        testFileInputOutputStream();
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
    //复制文件
    public static void testFileInputOutputStream(){
        File file1 = new File("C:\\Users\\Administrator\\Desktop\\music.rar");
        File file2 = new File("C:\\Users\\Administrator\\Desktop\\DEF.rar");

        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream(file1);
            fos = new FileOutputStream(file2);
            byte[] b = new byte[1024];
            int len;

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

            fis.close();
            fos.close();  
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }
    public static void testFileInputStream3(String counte){
        //输出
        File file = new File("C:\\Users\\Administrator\\Desktop\\DEF.txt");

        try {
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(counte.getBytes());

            fos.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public static void testFileInputStream2(){
        //使用数组
        File file = new File("C:\\Users\\Administrator\\Desktop\\ABC.txt");
        try {
            FileInputStream fis = new FileInputStream(file);
            byte[] b = new byte[5];//读取到的数据 
            int len;
            try {
                while((len = fis.read(b)) != -1){
//                  for (int i = 0; i < len; i++) {
//                      System.out.print((char)b[i]);
//                  }
                    String st = new String(b, 0, len);
                    System.out.print(st);
                }
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
    /**
     * 以字节方式读取文件(),并输出到控制台
     * */
    public static void testFileInputStream1(){
        File file = new File("C:\\Users\\Administrator\\Desktop\\ABC.txt");

        try {
            FileInputStream fis = new FileInputStream(file);
            //3、调用FileIn普通Stream的方法
            int b = fis.read();
            while (b != -1) {
                System.out.print((char)b);
                b = fis.read();
            }
            fis.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

2、FileOutputStream的使用

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class TestFileReaderWriter {

    public static void main(String[] args) {
//      testFileReader();
        testFileReaderWriter();
    }
    /**
     * 复制文本文件
     * */
    public static void testFileReaderWriter(){
        FileReader fr = null;
        FileWriter fw = null;

        File file1 = new File("C:\\Users\\Administrator\\Desktop\\123.txt");
        File file2 = new File("C:\\Users\\Administrator\\Desktop\\456.txt");

        try {
            fr = new FileReader(file1);
            fw = new FileWriter(file2);
            char[] c = new char[1024];
            int len;
            while((len = fr.read(c)) != -1){
                fw.write(c, 0, len);
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }


    }
    /**
     * 读取文本文件
     * */
    public static void testFileReader(){
        File file = new File("C:\\Users\\Administrator\\Desktop\\123.txt");
        FileReader fr = null;
        try {
            fr = new FileReader(file);
            char[] c = new char[1024];
            int len;
            while((len = fr.read(c)) != -1){
                String str = new String(c, 0, len);
                System.out.println(str);
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }

}

3、FileReaderWriter的使用

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class TestBufferedInputOutputStream {

    /**
     * 缓冲流
     * BufferedInputStream
     * BufferedOutputStream
     * BufferedReader
     * BufferedWriter
     */

    public static void main(String[] args) {
//      testBufferedInputOutputStream();
//      testBufferedReaderWriter();
        testBufferedReaderWriter2();
    }
    //缓冲流  文本文件的复制
    public static void testBufferedReaderWriter2(){
        File file1 = new File("C:\\Users\\Administrator\\Desktop\\123.txt");

        FileReader fr = null;
        BufferedReader br= null;

        try {
            fr = new FileReader(file1);
            br = new BufferedReader(fr);


            String len;

            while((len = br.readLine()) != null){

                System.out.println(len);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally { 
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }


    }
    public static void testBufferedReaderWriter(){
        File file1 = new File("C:\\Users\\Administrator\\Desktop\\123.txt");
        File file2 = new File("C:\\Users\\Administrator\\Desktop\\456.txt");

        FileReader fr = null;
        FileWriter fw = null;
        BufferedReader br= null;
        BufferedWriter bw = null;

        try {
            fr = new FileReader(file1);
            fw = new FileWriter(file2);

            br = new BufferedReader(fr);
            bw = new BufferedWriter(fw);

            char[] c = new char[1024];
            int len;

            while((len = br.read(c)) != -1){
                bw.write(c, 0, len);
                bw.flush();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }


    }
    //缓冲流  非文本文件的复制
    public static void testBufferedInputOutputStream(){
        File file1 = new File("C:\\Users\\Administrator\\Desktop\\123.txt");
        File file2 = new File("C:\\Users\\Administrator\\Desktop\\456.txt");
        //2、先创建相应的节点流,FileInputStream、FileInputStream
        FileInputStream fis;
        FileOutputStream fos;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            fis = new FileInputStream(file1);
            fos = new FileOutputStream(file2);
            //3、将创建的节点流的对象作为形参传递给缓冲流的构造器
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);

            byte[] b = new byte[1024];
            int len;
                while((len = bis.read(b)) != -1){
                    bos.write(b, 0, len);
                    bos.flush();
                }

            bis.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }






    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值