zoukankan      html  css  js  c++  java
  • Java 扫描器类 Scanner类

    1、Scanner是SDK1.5新增的一个类,可是使用该类创建一个对象.
    Scanner reader=new Scanner(System.in);  

    2、reader对象调用下列方法(函数),读取用户在命令行输入的各种数据类型:next.Byte(),nextDouble(),nextFloat,nextInt(),nextLine(),nextLong(),nextShot()上述方法执行时都会造成堵塞,等待用户在命令行输入数据回车确认.

    可以从字符串(Readable)、输入流、文件等等来直接构建Scanner对象,有了Scanner了,就可以逐段(根据正则分隔式)来扫描整个文本,并对扫描后的结果做想要的处理。

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class ScannerDemo {
    	public static void main(String[] args) throws Exception {
    		//扫描文件中的数据
    		//Scanner s=new Scanner(new File("file/text.txt"),"GBK");
    		//扫描键盘中的数据
    		Scanner s=new Scanner(System.in);
    		while(s.hasNextLine()){
    			String content=s.nextLine();
    			System.out.println("ECHO:"+content);
    		}
    		s.close();
    	}
    }
    



    三、Scanner默认使用空格作为分割符来分隔文本,但允许你指定新的分隔符

    使用默认的空格分隔符:
    public static void main(String[] args) throws FileNotFoundException {
                    Scanner s = new Scanner("123 asdf sd 45 789 sdf asdfl,sdf.sdfl,asdf    ......asdfkl    las");
    //                s.useDelimiter(" |,|\.");
                    while (s.hasNext()) {
                            System.out.println(s.next());
                    }
            }



    123
    asdf
    sd
    45
    789
    sdf
    asdfl,sdf.sdfl,asdf
    ......asdfkl
    las

    Process finished with exit code 0



    将注释行去掉,使用空格或逗号或点号作为分隔符,输出结果如下:
    123
    asdf
    sd
    45
    789
    sdf
    asdfl
    sdf
    sdfl
    asdf







    asdfkl

    las

    Process finished with exit code 0




    四、一大堆API函数,实用的没几个

    (很多API,注释很让人迷惑,几乎毫无用处,这个类就这样被糟蹋了,启了很不错的名字,实际上做的全是龌龊事)

    下面这几个相对实用:

    delimiter()
              返回此 Scanner 当前正在用于匹配分隔符的 Pattern。
    hasNext()
              判断扫描器中当前扫描位置后是否还存在下一段。(原APIDoc的注释很扯淡)
    hasNextLine()
              如果在此扫描器的输入中存在另一行,则返回 true。
    next()
              查找并返回来自此扫描器的下一个完整标记。
    nextLine()
              此扫描器执行当前行,并返回跳过的输入信息。



    五、逐行扫描文件,并逐行输出

    看不到价值的扫描过程
    public static void main(String[] args) throws FileNotFoundException {
                    InputStream in = new FileInputStream(new File("C:\AutoSubmit.java"));
                    Scanner s = new Scanner(in);
                    while(s.hasNextLine()){
                            System.out.println(s.nextLine());
                    }
            }



    package own;

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.HttpURLConnection;
    import java.net.ProtocolException;
    import java.net.URL;

    import com.verisign.uuid.UUID;

    /**
    * @author wangpeng
    *
    */
    public class AutoSubmit {

      /**
        * @param args
        * @throws Exception   
        */
      public static void main(String[] args) throws Exception {

    ...在此省略N行

    Process finished with exit code 0

  • 相关阅读:
    创建ros的程序包--3
    ROS文件系统介绍--2
    安装并配置ROS环境1
    ros-indigo-desktop-full安装到ubuntu14.04
    嵌入式声卡应用分析---18
    linux用户态定时器的使用---19
    tiny4412 linux+qtopia nfs网络文件系统的挂载
    ActiveMQ
    Web.xml配置详解之context-param
    Spring MVC的多视图解析器配置及与Freemarker的集成
  • 原文地址:https://www.cnblogs.com/jiangxifanzhouyudu/p/6726144.html
Copyright © 2011-2022 走看看