栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

什么是IO流,怎么使用IO流,缓冲区字节输出输入流,缓冲区字符输入输出流

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

什么是IO流,怎么使用IO流,缓冲区字节输出输入流,缓冲区字符输入输出流

字节流:二进制字节 字符流:字符 字节流向字符流转换 //字节流
 public static void main(String[] args) {

        //字节输入流
        FileInputStream fis = null;
        //字节输出流
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream("d:\bugs.jpg");
            fos = new FileOutputStream("d:\butif.jpg");
            //一次读取的数量1kb
            byte[] b = new byte[1024];
            //字节流读取数据
            int res = 0;
            while ((res = fis.read(b)) != -1) {
                System.out.println(res);
                fos.write(b, 0, res);
            }
        } catch (FileNotFoundException e) {
            System.out.println("找不到文件异常");
        } catch (IOException e) {
            System.out.println("流异常");
        } finally {
            try {
                fos.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
//缓冲区字节流
 public static void main(String[] args) {
        //字节输入流
        FileInputStream fis = null;
        //字节输出流
        FileOutputStream fos = null;
        //缓冲区
        BufferedInputStream bif = null;
        BufferedOutputStream bof = null;

        try {
            //fis = new FileInputStream("d:\bugs.jpg");
            //fos = new FileOutputStream("d:\butif.jpg");

            bif = new BufferedInputStream(new FileInputStream("d:\bugs.jpg"));
            bof = new BufferedOutputStream(new FileOutputStream("d:\butif.jpg"));

            //一次读取的数量1kb
            byte[] b = new byte[1024];
            //字节流读取数据
            int res = 0;
            while ((res = bif.read(b)) != -1) {
                System.out.println(res);
                bof.write(b, 0, res);
            }
        } catch (FileNotFoundException e) {
            System.out.println("找不到文件异常");
        } catch (IOException e) {
            System.out.println("流异常");
        } finally {
            try {
                bif.close();
                bof.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
//缓冲区字符流
public static void main(String[] args) {
        BufferedReader br = null;
        BufferedWriter bw = null;

        try {
            //输入
            br = new BufferedReader(new FileReader(new File("d:\作业.txt")));
            try {
                //输出
                bw = new BufferedWriter(new FileWriter(new File("d:\love.txt")));
                String trme = null;
                while ((trme =br.readLine())!=null){
                    bw.write(trme);//写入
                    bw.newline();//换行
                }
                bw.flush();//刷新缓冲区
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                br.close();
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/269941.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号