[Java iO] _ notes

Source: Internet
Author: User

[Java iO] _ notes

Objectives of this chapter:
Measure the test taker's knowledge about the role of the category class.
Use the receiver to receive input data

Introduction:

This class is not in the Java. Io package, but in the Java. util package, this class is a tool class

Use NotePad to receive input data from the keyboard:

Import Java. util. *; public class scannerdemo01 {public static void main (string ARGs []) {shortscan = new external (system. in); // receives data from the keyboard system. out. println ("input data:"); string STR = scan. next (); // receives data system. out. println ("the input data is:" + Str );}}

It is more convenient than directly using bufferedreader. However, the above input data code is actually problematic.
If there is a space between the input data, the space is used as the separator. If you want to enter a space, you must modify the delimiter to "\ n"
Public parameter usedelimiter (string pattern)

Modify the separator:

Import Java. util. *; public class scannerdemo02 {public static void main (string ARGs []) {shortscan = new external (system. in); // receives data from the keyboard system. out. print ("input data:"); scan. usedelimiter ("\ n"); string STR = scan. next (); // receives data system. out. println ("the input data is:" + Str );}};

The previous input data is returned in the form of a string. In fact, using the percentages class can also easily directly return integers or decimals.
You can use the following methods:
Receive INTEGER:
|-Judge whether the value is an integer: Public Boolean hasnextint (). If the value is an integer, true is returned.
|-Received data: Public int nextint ()
Receive decimals:
|-Determines whether it is decimal: Public Boolean hasnextfloat ()
|-Received data: public float nextfloat ()

Import Java. util. *; public class scannerdemo03 {public static void main (string ARGs []) {scan = new feature (system. in); // receive data from the keyboard int I = 0; float F = 0.0f; system. out. print ("input INTEGER:"); If (scan. hasnextint () {// determines whether the input is an integer I = scan. nextint (); // receives the integer system. out. println ("integer data:" + I);} else {system. out. println ("the input is not an integer! ");} System. out. print ("input decimal:"); If (scan. hasnextfloat () {// determines whether the input is decimal. f = scan. nextfloat (); // receives the decimal number system. out. println ("decimal data:" + F);} else {system. out. println ("the input is not a decimal number! ");}}};

Although the accept class can receive various types, it cannot receive data of the date type.
If you want to receive data of the date type, you can only perform string transformation. However, you can still use the methods provided in the date class to verify the received data:
Verification: Public String hasnext (string pattern)
Receiving: Public String next (string pattern)

Import Java. util. *; import Java. text. *; public class scannerdemo04 {public static void main (string ARGs []) {shortscan = new external (system. in); // receives data from the keyboard string STR = NULL; Date = NULL; system. out. print ("input date (yyyy-mm-dd):"); If (scan. hasnext ("^ \ D {4}-\ D {2}-\ D {2} $") {// verify STR = scan. next ("^ \ D {4}-\ D {2}-\ D {2} $ "); // receive try {date = new simpledateformat ("yyyy-mm-dd "). parse (STR);} Ca Tch (exception e) {}} else {system. Out. println ("the input date format is incorrect! ") ;}System. Out. println (date );}};

You can also directly read information from the file.
Suppose: D: \ test.txt File
Use volume to directly Read File Content

Import Java. util. *; import Java. text. *; import Java. io. *; public class scannerdemo05 {public static void main (string ARGs []) {file F = new file ("D:" + file. separator + "test.txt"); // specifies the operation File Upload scan = NULL; try {scan = new train (f); // receives data from the keyboard} catch (exception E) {} string STR = NULL; while (scan. hasnext () {STR = scan. next (); // fetch data} system. out. println ("File Content:" + Str );}};

The current file content is indeed read in, but this program is still not perfect. Only the first row of data can be read. Because there is a line break, it is deemed that the read is complete.

Import Java. util. *; import Java. text. *; import Java. io. *; public class scannerdemo05 {public static void main (string ARGs []) {file F = new file ("D:" + file. separator + "test.txt"); // specifies the operation File Upload scan = NULL; try {scan = new train (f); // receives data from the keyboard} catch (exception E) {} stringbuffer STR = new stringbuffer (); While (scan. hasnext () {Str. append (scan. next ()). append ('\ n'); // retrieve data} system. out. println ("File Content:" + Str );}};

When using the delimiter to read files, you must consider the line feed function.

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.