另类输入缓冲流-java.util.Scanner类
jdk1.5新增了java.util.Scanner类类似于C++里面的cin:
Constructor Summary
Scanner(File source)
Constructs a new Scanner that produces values scanned from the specified file.
Scanner(File source, String charsetName)
Constructs a new Scanner that produces values scanned from the specified file.
Scanner(InputStream source)
Constructs a new Scanner that produces values scanned from the specified input stream.
Scanner(InputStream source, String charsetName)
Constructs a new Scanner that produces values scanned from the specified input stream.
Scanner(Readable source)
Constructs a new Scanner that produces values scanned from the specified source.
Scanner(ReadableByteChannel source)
Constructs a new Scanner that produces values scanned from the specified channel.
Scanner(ReadableByteChannel source, String charsetName)
Constructs a new Scanner that produces values scanned from the specified channel.
Scanner(String source)
Constructs a new Scanner that produces values scanned from the specified string.
Method Summary
void close()
Closes this scanner.
Pattern delimiter()
Returns the Pattern this Scanner is currently using to match delimiters.
String findInLine(Pattern pattern)
Attempts to find the next occurrence of the specified pattern ignoring delimiters.
String findInLine(String pattern)
Attempts to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.
String findWithinHorizon(Pattern pattern, int horizon)
Attempts to find the next occurrence of the specified pattern.
String findWithinHorizon(String pattern, int horizon)
Attempts to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.
boolean hasNext()
Returns true if this scanner has another token in its input.
boolean hasNext(Pattern pattern)
Returns true if the next complete token matches the specified pattern.
boolean hasNext(String pattern)
Returns true if the next token matches the pattern constructed from the specified string.
boolean hasNextBigDecimal()
Returns true if the next token in this scanner’s input can be interpreted as a BigDecimal using the nextBigDecimal() method.
boolean hasNextBigInteger()
Returns true if the next token in this scanner’s input can be interpreted as a BigInteger in the default radix using the nextBigInteger() method.
boolean hasNextLine()
Returns true if there is another line in the input of this scanner.
boolean hasNext…(int radix)
Returns true if the next token in this scanner’s input can be interpreted as a … value in the specified radix using the next…() method.
… <=> 所有数据类型;
… next…(); // … <=> 所有数据类型;
IOException ioException()
Returns the IOException last thrown by this Scanner’s underlying Readable.
Locale locale()
Returns this scanner’s locale.
MatchResult match()
Returns the match result of the last scanning operation performed by this scanner.
String next()
Finds and returns the next complete token from this scanner.
String next(Pattern pattern)
Returns the next token if it matches the specified pattern.
String next(String pattern)
Returns the next token if it matches the pattern constructed from the specified string.
int radix()
Returns this scanner’s default radix.
void remove()
The remove operation is not supported by this implementation of Iterator.
Scanner reset()
Resets this scanner.
Scanner skip(Pattern pattern)
Skips input that matches the specified pattern, ignoring delimiters.
Scanner skip(String pattern)
Skips input that matches a pattern constructed from the specified string.
String toString()
Returns the string representation of this Scanner.
Scanner useDelimiter(Pattern pattern)
Sets this scanner’s delimiting pattern to the specified pattern.
Scanner useDelimiter(String pattern)
Sets this scanner’s delimiting pattern to a pattern constructed from the specified String.
Scanner useLocale(Locale locale)
Sets this scanner’s locale to the specified locale.
Scanner useRadix(int radix)
Sets this scanner’s default radix to the specified radix.
——接收标准输入:
package com.mldn;
import java.util.Scanner;
public class ScannerDemo
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in); // 接收标准输入
scan.useDelimiter(“\n”); // 以回车为分隔符
System.out.println(“请输入内容:”);
System.out.println(“输入内容为:\n” + scan.next()); // 接收字符串
System.out.println(“请输入整数:”);
if (scan.hasNextInt()) // 判断输入是否为整型
{
System.out.println(scan.nextInt());
}
else
{
System.out.println(“输入的不是整数!”);
}
}
}
/*
administrator@xu-desktop:~$ java com.mldn.ScannerDemo
请输入内容:
中国 等待 dkdk 23402858@# %5^^6&&~~
输入内容为:
中国 等待 dkdk 23402858@# %5^^6&&~~
可以接收来自键盘的各种类型的数据;
*/
——–接收磁盘文件数据:
package com.mldn;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class Scan
{
public static void main(String[] args)
{
String path =File.separator + “home” + File.separator + “administrator” + File.separator + “111”;
File file = new File(path); // 实例化文件
Scanner sc = null; // 声明扫描器
try
{
sc = new Scanner(file); // 从文件读取
}
catch (IOException e)
{
e.printStackTrace();
}
StringBuffer buf = new StringBuffer(); // 接收数据
while (sc.hasNextLine()) // 还有新行,往下读
{
buf.append(sc.nextLine()).append(“\n”); // 添加换行
}
sc.close(); // 关闭扫描器
System.out.print(buf); // 输出到标准输出
}
}
/*
administrator@xu-desktop:~$ java com.mldn.Scan
毛峰属于绿茶,产地很多,主要有云南,峨眉,遵义等,但正宗原产地为安徽黄山,也是我国十大名茶之一。
黄山毛峰属于炒青绿茶,外形微卷,状似雀舌,绿中泛黄,银毫显露,且带有金黄色鱼叶(俗称黄金片)。入杯冲泡雾气结顶,汤色清碧微黄,叶底黄绿有活力,滋味醇甘,香气如兰,韵味深长。
雀舌是黄山毛峰中的极品,产于明前第一批茶。
形状: 芽头肥壮,形似雀舌
色泽: 嫩绿泛象牙色
净度: 匀齐
香气: 嫩香持久
滋味: 鲜爽回甘
汤色: 淡黄清澈明亮
叶底: 嫩匀肥壮,嫩绿明亮
*/
———在使用上,Scanner很方便,但是效率上往往比不上BufferedReader,开发中后者常用实现底层!
声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 嗅谱网
转载请注明:转自《另类输入缓冲流-java.util.Scanner类》
本文地址:http://www.xiupu.net/archives-99.html
关注公众号:
微信赞赏
支付宝赞赏