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

io流(课堂笔记4)

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

io流(课堂笔记4)

1.流

流:在java数据中所有数据都是使用流读写的。流是一种有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两个设备间的传输成为流,流的本质是数据传输。

流分为:输出流,输入流。

2.字节流,字符流

字节流:数据流中最小的数据单元是字节。 字符流:数据中最小的数据单元是字符,一个字符占用两个字节。

 3.字节流

public class ByteStream {
    public static void main(String[] args) {
        //输入
        FileInputStream fis=null;
        //输出
        FileOutputStream fos=null;
        try {
            fis=new FileInputStream("D:\abc.txt");
            fos=new FileOutputStream("D:\ab.txt");
            byte[] b=new byte[1024];
            int c=0;
            while ((c=fis.read(b))!=-1){
                fos.write(b,0,c);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fis.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

4.字符流
public class CharacterStream {
    public static void main(String[] args) {
        //输入
        FileReader fir=null;
        //输出
        FileWriter fil=null;
        BufferedReader br=null;
        BufferedWriter bw=null;
        try {
            fir=new FileReader(new File("D:\abc.txt"));
            fil=new FileWriter(new File("D:\ac.txt"));
            br=new BufferedReader(fir);
            bw=new BufferedWriter(fil);
            String temp=null;
            while ((temp=br.readLine())!=null){
                bw.write(temp);
                //换行
                bw.newline();
            }
            //刷新缓冲区
            bw.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fir.close();
                fil.close();
                br.close();
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
}

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

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

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