1、Scanner介绍
2、构造方法
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Scanner;
public class Demo3_Scanner {
public static void main(String[] args) throws FileNotFoundException {
//构造方法
//1、Scanner(InputStream) 扫描流对象(键盘扫描)
InputStream in = System.in;
Scanner input = new Scanner(in);
System.out.println(input);//java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]
//2、Scanner(File) 扫描文本
File file = new File("/Users/jim/Desktop/Work/temp/mail_htmltemplate.txt");
Scanner input2 = new Scanner(file);
System.out.println(input2);//java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]
//3、Scanner(String) 扫描 字符串
Scanner input3 = new Scanner("good good study,day day up");
System.out.println(input3);//java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]
}
}
3、功能方法
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Scanner;
public class Demo3_Scanner {
public static void main(String[] args) throws FileNotFoundException {
//扫描文本
File file = new File("/Users/jim/Desktop/Work/temp/mail_htmltemplate.txt");
Scanner input2 = new Scanner(file);
String str = input2.nextLine();
System.out.println(str);//<!doctype html>
//扫描字符串
Scanner input3 = new Scanner("good good study,day day up");
String s = input3.next();
System.out.println(s);//good
//method_hasNextXxx();
//method_nextXxx();
// method_构造方法();
}
private static void method_hasNextXxx() {
//hasNextXxx() 判断扫描的值类型是否吻合,返回true/false
Scanner input = new Scanner(System.in);
if (input.hasNextInt()) {
int i = input.nextInt();//java.util.InputMismatchException 输入不吻合错误
System.out.println(i);
}
//close() 释放资源(操作系统资源)
input.close();
}
private static void method_nextXxx() {
//.nextXxx()
Scanner input = new Scanner(System.in);
int i = input.nextInt();
System.out.println(i);//10
double d = input.nextDouble();
System.out.println(d);//2.3
boolean b = input.nextBoolean();
System.out.println(b);//true
//获取字符串首字符
char c = input.next().charAt(0);
System.out.println(c);
//nextLine() 扫描字符串数据,但如果与其他nextXxx一起使用时会导致无法扫描字符串。
//问题:如何形成?
//结束next()扫描的方式? 回车键,空格(空字符如tab)
//结束nextLine()扫描的方式?只能回车键
//>>>因为nextXxx录入数据时的回车键被nextLine()所获取,导致nextLine直接结束,无法扫描字符串。
//解决: 1、将所有的nextLine换成next
// 2、重新获取Scannner对象
// 3、多写一次nextLine()
//String str = input.next();
//System.out.println(str);
//input = new Scanner(System.in);
input.nextLine();//上面留下来的获取回车符
String s = input.nextLine();
System.out.println(s);
}