【0基础学java】教学日志:javaSE-IO流 2

本章概述:本章主要讲了BufferedWriter类,BufferedReader类,BufferedWriterDemo类的使用,以及从万维网上摘取百度网页的操作

目录

本章概述:

一、BufferedWriter的使用

1、BufferedWriterDemo

二、BufferedReader类

1、BufferedReaderDemo

三、BufferedWriter的使用

1、BufferedWriterDemo

四、从万维网上摘取百度网页的操作

1、BaiduDemo


本章概述:

一、BufferedWriter的使用

1、BufferedWriterDemo

package com.tensent.class4.readerAndwriter;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @Auther: Yu Panpan
 * @Date: 2022/4/12 - 04 - 12 - 14:58
 * @Description: com.tensent.class4.readerAndwriter
 * @version: 1.0
 */
public class BufferedWriterDemo {


    public static void main(String[] args) {
        FileWriter fileWriter = null;
        BufferedWriter bufferedWriter = null;

        try {
            fileWriter = new FileWriter("d3.txt");
            bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(101);
            bufferedWriter.write("www.baidu.com百度".toCharArray());
            //写入一个换行
            bufferedWriter.newLine();
            bufferedWriter.write("今天天气不错!");
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

二、BufferedReader类

1、BufferedReaderDemo

package com.tensent.class4.readerAndwriter;

import com.sun.media.sound.SoftTuning;

import java.io.*;

/**
 * @Auther: Yu Panpan
 * @Date: 2022/4/12 - 04 - 12 - 14:58
 * @Description: com.tensent.class4.readerAndwriter
 * @version: 1.0
 */
public class BufferedReaderDemo {

    public static void main(String[] args) {
        FileReader fileReader = null;
        BufferedReader reader = null;

        try {
            fileReader = new FileReader("a1.txt");
            reader=  new BufferedReader(fileReader);
//            int length = 0;
//            char[] chars = new char[1024];
//            while((length = reader.read(chars))!=-1){
//                System.out.println(new String(chars,0,length));
//            }
//            String s = reader.readLine();
//            System.out.println(s);
//            s = reader.readLine();
//            System.out.println(s);
//            s = reader.readLine();
//            System.out.println(s);
//            s = reader.readLine();
//            System.out.println(s);
//            s = reader.readLine();
//            System.out.println(s);
              String str = "";
              while((str = reader.readLine())!=null){
                  System.out.println(str);
              }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

三、BufferedWriter的使用

1、BufferedWriterDemo

package com.tensent.class4.readerAndwriter;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @Auther: Yu Panpan
 * @Date: 2022/4/12 - 04 - 12 - 14:58
 * @Description: com.tensent.class4.readerAndwriter
 * @version: 1.0
 */
public class BufferedWriterDemo {


    public static void main(String[] args) {
        FileWriter fileWriter = null;
        BufferedWriter bufferedWriter = null;

        try {
            fileWriter = new FileWriter("d3.txt");
            bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(101);
            bufferedWriter.write("www.baidu.com百度".toCharArray());
            //写入一个换行
            bufferedWriter.newLine();
            bufferedWriter.write("今天天气不错!");
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

四、从万维网上摘取百度网页的操作

1、BaiduDemo

package com.tensent.class4.exercise;

import java.io.*;
import java.net.URL;

/**
 * @Auther: Yu Panpan
 * @Date: 2022/4/12 - 04 - 12 - 15:44
 * @Description: com.tensent.class4.exercise
 * @version: 1.0
 */
public class BaiduDemo {

    public static void main(String[] args) {
        BufferedReader bufferedReader = null;
        BufferedWriter bufferedWriter = null;

        try {
            bufferedReader = new BufferedReader(new InputStreamReader(new URL("http://www.baidu.com").openStream(),"utf-8"));
            bufferedWriter = new BufferedWriter(new FileWriter("D:\\baidu.html"));
            String str = "";
            while(( str = bufferedReader.readLine() )!=null){
                bufferedWriter.write(str);
                bufferedWriter.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jason的java世界

不要吝啬你的赞赏哦~~~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值