【Java之竞赛必备】——BufferedReader和BufferedWriter(快读快写)

🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇

                                 Java之--快读快写                            

🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇

今日推荐歌曲: Keep Your Head Up Princess  🎵🎵


目录

1. Scanner和System.out 慢的原因

2. BufferedReader快的原因

3. 快读快些模板(内含详细解析)

4. 总结(模板)


  模板

import java.util.*;
import java.io.*;

public class Main
{
    public static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static Read in = new Read();
    
    public static void main(String[] args) throws IOException
    {
        // 写代码

        out.close();
    }
}


class Read // 自定义快速读入
{
    StringTokenizer st = new StringTokenizer("");
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    String next() throws IOException 
    {
        while(!st.hasMoreTokens())
        {
            st = new StringTokenizer(bf.readLine());
        }
        return st.nextToken();
    }
    
    String nextLine() throws IOException 
    {
        return bf.readLine();
    }
    
    int nextInt() throws IOException 
    {
        return Integer.parseInt(next());
    }
    
    long nextLong() throws IOException 
    {
        return Long.parseLong(next());
    }
    
    double nextDouble() throws IOException 
    {
        return Double.parseDouble(next());
    }
}

1. Scanner和System.out 慢的原因


首先要知道Java处理IO有两套标准: 1.字节流   2.字符流

为什么Java 的Scanner或者System.out慢?

  • 这是因为这两个方法读写的时候,每次只能访问或者输入一个值,每次都要经过访问IO设备文件之后再操作一个值,所以当需要操作的值很多的时候,就会经过大量的访问IO设备,所以读写速度会很慢.
  • Scanner 和 System.out是属于 字节流 IO


2. BufferedReader快的原因

  • 会先将IO设备里的数据先放进内存缓冲区里边,然后读写的时候从内存缓冲区里边一次性读写这些数据,那么就是说只用访问一次IO设备就可以读写到数据,速度效率就会极大的提高 !  !  !
  • 这里BufferedReader将  字节流->字符流 每次读取都是一行读取并且只用访问一次IO设备.


3. 快读快些模板(内含详细解析)

import java.util.*;
import java.io.*;
//记得包含io的包
public class Main
{
    //先把 System 的  字节流->字符流
    //用 PrintWriter 包装是因为里边的方法和 sout 相同 便于使用
    //这里写入不用自定义
    public static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static Read in = new Read();
    
    public static void main(String[] args) throws IOException
    {
        // 写代码

        out.close();
    }
}


class Read // 自定义快速读入
{
    //StingTokenizer用来裁剪 从BufferedReader字符串数据行中 裁剪成一个一个的字符串 和Scanner 里的in.next()一样;
    StringTokenizer st = new StringTokenizer("");
    //把 System 的  字节流->字符流 将所读写的数据存到 BufferReader读入缓冲区里
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

    //接下来定义默认方法 类似与Scanner里的nextLine,nextInt等

    //这里的next 是配合下面方法nextInt,nextLong方法使用的,从缓冲区的字符串数据中截取一个一个的字符串转换成相应类型
    //例如:"1 2 44 5" 这里将这一串字符串转化成一个个"1" "2" "44" "5" 来给下边的nextInt用parseInt来转化成整形 1,2,44,5;
    String next() throws IOException 
    {
        //这里利用 while 循环来获取多行数据
        while(!st.hasMoreTokens())
        {
            st = new StringTokenizer(bf.readLine());
        }
        return st.nextToken();
    }

    //直接从缓冲区中读取一行字符串
    String nextLine() throws IOException 
    {
        return bf.readLine();
    }
    
    int nextInt() throws IOException 
    {
        return Integer.parseInt(next());
    }
    
    long nextLong() throws IOException 
    {
        return Long.parseLong(next());
    }
    
    double nextDouble() throws IOException 
    {
        return Double.parseDouble(next());
    }
}

4. 总结(模板)

import java.util.*;
import java.io.*;

public class Main
{
    public static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static Read in = new Read();
    
    public static void main(String[] args) throws IOException
    {
        // 写代码

        out.close();
    }
}


class Read // 自定义快速读入
{
    StringTokenizer st = new StringTokenizer("");
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    String next() throws IOException 
    {
        while(!st.hasMoreTokens())
        {
            st = new StringTokenizer(bf.readLine());
        }
        return st.nextToken();
    }
    
    String nextLine() throws IOException 
    {
        return bf.readLine();
    }
    
    int nextInt() throws IOException 
    {
        return Integer.parseInt(next());
    }
    
    long nextLong() throws IOException 
    {
        return Long.parseLong(next());
    }
    
    double nextDouble() throws IOException 
    {
        return Double.parseDouble(next());
    }
}

真嘟很详细,手敲原创的 ,希望能帮助大伙,博客不易,点赞 收藏 加关注,知识进脑不迷路!!!

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值