JAVA Basics------IO Operations (iii) __java

Source: Internet
Author: User

Scanner class

In fact, we are more commonly used to use the scanner class to enter data, the following to give a scanner example bar

   
   
    
    import java.util.scanner;
    
       /**    * scanner Small example, reading data from the keyboard    * */  public class scannerdemo{      public static void mai N (String[] args) {          SCANNER SCA = NEW&N Bsp Scanner (system.in);           //  read an integer       &NBS P;   int temp = sca.nextint ();           SYSTEM.OUT.PRINTLN (temp);           //Read floating-point numbers        &nbs P;  float flo=sca.nextfloat ();           SYSTEM.OUT.PRINTLN (FLO);     &NBSp;     //read characters           //... And so on, are some too basic, it is not normal.
   
          }  } 

In fact, scanner can accept any input stream

Here's a read from a file using the scanner class

   
   
    
    Import Java.io.File;  
    
    import java.io.FileNotFoundException;  
    
    import Java.util.Scanner;
    
    /**
    
    * Scanner Small example, read from file
    
    * *
    
    /public class scannerdemo{public
    
    static void main (S Tring[] args {
    
    file File = new File ("D:" + File.separator + "Hello.txt");
    
    

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.