编程知识 cdmana.com

Java - IO scan stream: scanner

IO_ Scan stream :Scanner

In terms of improving the insufficient output function, the print stream is provided (PrintSream); utilize BufferedReader class ( Buffered input stream ) Solve the input and read operation of large text data , however BufferedReader Class can only read data as strings (public String readLine();) And fixed use line feed \n As a separator , This reveals that BufferedReader The function of class is not enough for future development .

Scanner class

  • java.util.Scanner:(JDK 1.5
    • Specifically responsible for solving input stream operation problems

    public final class Scanner extends Object implements Iterator<String>

  • Construction method :
public Scanner(File source)
public Scanner(InputStream source)
public Scanner(Readable source)
public Scanner(ReadableByteChannel source)
public Scanner(String source)
  • Class method

Determine whether there is specified data :

public boolean hasNextXxx();
//  give an example :
public boolean hasNext();// It means there is data 
public boolean hasNextDouble();// The input is decimal 

Take out the data :

public String nextXxx();
//  give an example :
public String next();// Default return string 
public double nextDouble();// Auto transition back double data 

Besides using hasNextXxx() and nextXxx() Method to judge the data , stay hasNext() and next() Methods support the use of regular expressions to judge data

public boolean hasNext(regax);
public String next(regax);

example :

public class TestDemo {
	public static void main(String [] args) throws IOException {
		//  Ready to receive keyboard input data ;System.in yes InputStream type 
		Scanner scan = new Scanner(System.in);
		if (scan.hasNextString()) {// Judge whether there is data 
			System.out.println(scan.next()); // Output 
		}
		scan.close();
	}
} 
  • Set separator
public Scanner useDelimiter(String pattern);
public class TestDemo { 
	public static void main(String [] args) throws IOException {
		Scanner scan = new Scanner(new FileInputStream(new File("F:"+File.separator+"demo"+File.separator+"demo.txt")));
		scan.useDelimiter("\r\n");// Set up  、\r\n  As a line break 
		while(scan.hasNext()) {
			System.out.println(scan.next());
		}
		scan.close();
	}
} 

summary

While reading the data , Comprehensive perspective :Scanner class Than BufferedReader Simple ; Found in Scanner When it is not possible to use BufferedReader

  • InputStream The problem of insufficient functionality of the class is Scanner Class resolution
  • Reader The problem of insufficient functionality of the class is BufferedReader Class resolution
  • OutputStream The problem of insufficient functionality of the class is PrintStream Class resolution
  • Writer The problem of insufficient functionality of the class is PrintWrite Class resolution

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[Mirror Wang Yuyang]所创,转载请带上原文链接,感谢

Scroll to Top