🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇
Java之--快读快写
🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇🎇
今日推荐歌曲: Keep Your Head Up Princess 🎵🎵
目录
模板
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());
}
}
真嘟很详细,手敲原创的 ,希望能帮助大伙,博客不易,点赞 收藏 加关注,知识进脑不迷路!!!