JavaIO流——节点流

FileReader

FileReader读取数据

注意点:一个流打开,在我们使用完之后一定要关闭流。

​ 在新建流、读取流、关闭流都需要抛出异常,这里抛出异常的方法必须是try-catch-finally

  1. 创建一个File类的对象,将需要读取的文件路径作为参数传入构造器
  2. 创建一个FileReader类的对象,将上述File类的对象作为参数传入构造器
  3. 调用FileReader.read(),返回结果是整型,将他转换为字符,如果都不到字符则返回-1
  4. 调用FileReader.cloce()关闭流
 public static void main(String[] args){
        FileReader fileReader = null;
        try {
            //1.新建一个File类的对象
            File file = new File("IOStream\\hello.txt");
            //2.将需要读取的File类对象作为参数传到FileReader构造器中
            fileReader = new FileReader(file);
            //3.调用read(),得到一个字符的整形格式,如果没有字符则返回-1
            int read = fileReader.read();
            while (read != -1){
                System.out.print((char) read);
                read = fileReader.read();
            }
            //优化写法
//            int data;
//            while ((data = fileReader.read()) != -1){
//                System.out.print((char) data);
//            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭字符流
            try {
                if(fileReader != null)
                     fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

FileReader读取数据(char[])

@Test
    public void test01() {
        FileReader fr = null;
        try {
            //1.创建File类对象
            File file = new File("hello.txt");
            //2.创建流的对象
            fr = new FileReader(file);
        /*
        3.读入数据操作
        fr.read(char[] cbuf)
           1.定义一个char数组来读取文本数据,char数组的长度则是一次最多能读取多少个字符
           2.定义一个len来读取一次读取了多少个字符
           3.当读不到字符时,则返回-1
         */
            char[] cbuf = new char[5];
            int len;
            while((len = fr.read(cbuf)) != -1){
                //注意点:每次遍历的长度应该是这次读取到了字符的长度,而不是cbuf数组的长度
                //第一种写法:for循环
//                for (int i = 0; i < len; i++) {
//                    System.out.print(cbuf[i]);
//                }
                //第二种:String构造器,要注意的是每次读取char[]应该是从0-len
                String str = new String(cbuf,0,len);
                System.out.print(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭流
            try {
                if(fr != null)
                    fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

FileWriter

  1. 创建File类的对象,指定需要写入数据文件的路径
  2. 创建FileWriter的对象,将File对象作为参数传入
    • 这里调用new FileWriter(file)或new FileWriter(file,false):则表示覆盖原来的文件
    • 这里调用new FileWriter(file,true):则表示在原来的文件上追加写入
  3. 调用write方法,写入数据
  4. 关闭流
 @Test
    public void test02() {
        FileWriter fw = null;
        try {
            //1.创建File类的对象,指定需要写入数据文件的路径
            File file = new File("helloworld.txt");
            //2.创建FileWriter的对象,将File对象作为参数传入
            //这里调用new FileWriter(file)或new FileWriter(file,false):则表示覆盖原来的文件
            //这里调用new FileWriter(file,true):则表示在原来的文件上追加写入
            fw = new FileWriter(file,true);

            //3.调用write方法,写入数据
            fw.write("I have a dream!\t");
            fw.write("I have a ball!\n");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭流
            try {
                if(fw != null)
                    fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

利用FileReader和FileWriter实现复制文件操作

@Test
    public void test03() {
        FileReader fr = null;
        FileWriter fw = null;
        try {
            File srcFile = new File("helloworld.txt");
            File destFile = new File("hello1.txt");

            fr = new FileReader(srcFile);
            fw = new FileWriter(destFile);

            char[] cbuf = new char[5];
            int len;
            while ((len = fr.read(cbuf)) != -1){
                fw.write(cbuf,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(fw != null)
                    fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(fr != null)
                    fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

InputStream以及OutputStream

使用InputStream、OutputStream实现图片的复制

@Test
    public void test01(){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //1.造文件对象
            File srcFile = new File("img.png");
            File destFile = new File("img1.png");

            //2。造流
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);

            //3.复制图片
            byte[] buffer = new byte[5];
            int len;//用来记录一次读取了多少条数据
            while ((len = fis.read(buffer)) != -1){
                fos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭流
            try {
                if(fos != null)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(fis != null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值