Java学习Day17 IO 节点流 处理流 字符流 字节流

Java学习Day17 IO 节点流 处理流 字符流 字节流

请添加图片描述

节点流NodeStream

字节输入流 FileInputStream

//字节输入流
//类名不能和FileInputStream同名
public class FileInputStream_ {
    public static void main(String[] args) {

    }

    @Test
    //单个字节的读取效率低
    public void readFile01() throws IOException {
        String filePath = "g:\\hello.txt";
        int readData = 0;
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(filePath);
            //返回-1,表示读取完毕 一个文件不止一个用While语句
            //不能正确显示中文 因为一个中文是三个字节 每个字节不能正确显示
            while ((readData = fileInputStream.read()) != -1) {
                System.out.print((char) readData);//转出char显示
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {//关闭释放资源,还要抛出异常
            fileInputStream.close();
        }
    }
    @Test
    //使用read(byte )提高效率
    public void readFile02() throws IOException {
        String filePath = "g:\\hello.txt";
        int readData = 0;
        byte[] buf = new byte[8];
        int readLen = 0;
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(filePath);
            //从该输入流读取b.length字节到字节数组 此方法将堵塞 直到某些输入可以结束
            //返回-1 表示读取完毕 每次读取8个字节
            //不能正确显示中文 因为一个中文是三个字节 每个字节不能正确显示
            while ((readLen = fileInputStream.read(buf)) != -1) {
                System.out.print(new String(buf,0,readLen));//转出char显示
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {//关闭释放资源,还要抛出异常
            fileInputStream.close();
        }
    }
}

字节输出流FileOutputStream

public class FileOutputStream_ {
    public static void main(String[] args) {
        
    }
    @Test
    //向文件中输入数据 若不存在就创建
    public void writeFile() throws IOException {
        FileOutputStream fileOutputStream = null;
        String filePath = "g:\\a.txt";
        try {
            //这种创建方式会覆盖原来的内容
            //注意这里的覆盖是指覆盖原本txt文件的内容
            fileOutputStream = new FileOutputStream(filePath);
            //追加文件
            //fileOutputStream = new FileOutputStream(filePath,true);
            //写入一个字节
            fileOutputStream.write('a');
            //写入一个字符串
            String str ="hello,world";
            fileOutputStream.write(str.getBytes()/*字符串转成字符数组*/);
            //             0是从第一个字符开始输入   写入到那个字符 可以设置字符大小
            fileOutputStream.write(str.getBytes(),0,str.length());
            fileOutputStream.write(str.getBytes(),0,3);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            fileOutputStream.close();
        }
    }
}

字符输入流

public class FileReader_ {
    public static void main(String[] args) throws FileNotFoundException {
        //1.创建FileReader对象
        String filePath = "g:\\story.txt";
        FileReader fileReader = null;
        int data = 0;
        try {
            fileReader = new FileReader(filePath);
            while ((data = fileReader.read()) != -1) {
                System.out.print((char) data);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileReader != null) {
                    fileReader.close();//使用FileRead流必须关闭或者刷新(flash)否则写入不成功
                }
            } catch (IOException e) {

                e.printStackTrace();
            }
        }

    }

    @Test
    public void readFile() {
        String filePath = "g:\\story.txt";
        FileReader fileReader = null;

        char[] buf =new char[8];//使用数组读取
        int readLen = 0;
        try {
            fileReader = new FileReader(filePath);
            //返回-1说明文件被循环读取到最后
            while ((readLen = fileReader.read(buf)) != -1) {
                System.out.print(new String(buf,0,readLen));
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileReader != null) {
                    fileReader.close();
                }
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
    }
}

字符输出流

public class FileWriter_ {
    public static void main(String[] args) throws IOException {
        //创建FileWriter 对象
        String filePath = "g:\\note.txt";
        FileWriter fileWriter = null;
        char[] chars ={'a','b','c'};
        try {
            fileWriter = new FileWriter(filePath);//文件覆盖写入
            //写入单个字符
            fileWriter.write('H');
            //写入数组
            fileWriter.write(chars);
            //写入数组的指定内容
            fileWriter.write("御狐王".toString(),0,3);
            //写入字符串
            fileWriter.write("北京欢迎你");
            //写入字符串的指定内容
            fileWriter.write("上海",0,2);
            //数据量大 就循环操作

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            fileWriter.close();//close or flush 才能真正的把数据写入
            //重要!比如语音采集程序如果不关闭文件流就G
            /*源码
                private void writeBytes() throws IOException {
        bb.flip();
        int lim = bb.limit();
        int pos = bb.position();
        assert (pos <= lim);
        int rem = (pos <= lim ? lim - pos : 0);

        if (rem > 0) {
            if (ch != null) {
                int wc = ch.write(bb);
                assert wc == rem : rem;
            } else {
                out.write(bb.array(), bb.arrayOffset() + pos, rem);
            }
        }
        bb.clear();
    }

             */
        }
    }
}

Copy

public class FileCopy {
    public static void main(String[] args) throws IOException {
        //1.创建文件输入流 把要copy的文件读入java
        //2.创建文件输出流 把要copy的文件写入到指定位置
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        String srcFilePath = "C:\\Users\\Administrator\\Desktop\\壁纸包\\1.png";
        String copFilePath = "G:\\1.png";
        try {
            fileInputStream = new FileInputStream(srcFilePath);
            fileOutputStream = new FileOutputStream(copFilePath);
            byte[] buf = new byte[1024];
            int readLen = 0;
            while ((readLen = fileInputStream.read(buf)) != -1) {
                fileOutputStream.write(buf, 0, readLen);//使用这个方法成功
                //如果直接使用buf 不给后面赋值有时候数据不够大小就会出错
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //完成文件拷贝
}

处理流BufferedStream

/*Buffer处理流扩展性好
BufferedRead类中有属性Reader 可以封装一个节点流 且该节点流是任意的只要是Reader子类
所以封装的任意数组 数据源都可以 即父类引用指向子类对象
public class BufferedReader extends Reader {
    private Reader in;//引用父类 指向父类的子类对象 相当于引用
    其余同子类的对象
}

字符输入流

/*
一般用于读取文本文件 不能是二进制文件包括音频图片视频等否则会破坏文件内容
 */
public class BufferedReader_ {
    public static void main(String[] args) throws IOException {
        String filePath ="G:a.java";
        //创建bufferedReader
        BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
        //读取
        String line;//按行读取效率高
        while ((line = bufferedReader.readLine())!=null)//当返回null时读取完毕
        {
            System.out.println(line);
        }
        //只需要关闭BufferedReader因为底层会自动关闭节点流
        bufferedReader.close();
        /*
            public void close() throws IOException {
        synchronized (lock) {
            if (in == null)
                return;
            try {
                in.close();//in就是FileReader 说明关闭处理流会自动关闭字节流
            } finally {
                in = null;
                cb = null;
            }
        }
    }
         */
    }
}

字符输出流

public class BufferedWriter_ {
    public static void main(String[] args) throws IOException {
        String filePath ="G:\\BufferedWriter.txt";// true表示在文件内追加而不是覆盖
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath,true));
        bufferedWriter.write("hello world\n");
        bufferedWriter.write("hello world");
        bufferedWriter.newLine();//和系统相关的换行符
        bufferedWriter.write("hello world\n");
        bufferedWriter.close();

    }
}

Copy

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

        //以下是字符流
        //不要操作二进制文件 音频 视频 文档
        String srcFilePath = "g:\\a.java";//源文件
        String destFilePath = "g:\\a2.java";//复制的文件

        BufferedReader br =null;
        BufferedWriter bw =null;
        //新建字字符操作
        br =new BufferedReader(new FileReader(srcFilePath));
        bw =new BufferedWriter(new FileWriter(destFilePath));

        String line;
        //readline读取一行的内容不带换行符
        while ((line =br.readLine())!=null){
            bw.write(line);
            //插入一个换行符
            bw.newLine();

        }
        System.out.println("Ok");
        if (br != null) {
            br.close();
        }
        if (bw != null) {
            bw.close();
        }
    }
}

Copy2

//使用字节流对图片的拷贝 当然也可以处理文本但是数字空间要大
public class BufferedCopy02 {
    public static void main(String[] args) throws IOException {
        String srcFilePath = "C:\\Users\\Administrator\\Desktop\\壁纸包\\1.png";
        String destFilePath = "G:\\1.png";

        //创建对象
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        //FileInputStream继承了InputStream 是子类
        bufferedInputStream = new BufferedInputStream(new FileInputStream(srcFilePath));
        bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(destFilePath));
        //循环地读取文件 并且写入dest
        byte[] buff =new byte[1024];
        int readLen =0;//读取了多大的长度
        //当返回-1表示文件读取完毕
        while((readLen = bufferedInputStream.read(buff))!=-1){
            bufferedOutputStream.write(buff,0,readLen);
        }
        //关闭外层处理流
        if (bufferedInputStream != null) {
            bufferedInputStream.close();
        }
        if (bufferedOutputStream != null) {
            bufferedOutputStream.close();
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值