Java BASIC Program Structure design basic types of input and output

Source: Internet
Author: User

Read input: Java.util.Scanner

A simple text scanner that can use regular expressions to parse basic types and strings.

Know:

Scanner uses the delimiter mode to break its input into markup, which by default matches whitespace. You can then use a different next method to convert the resulting token to a different type of value.

For example, the following code enables a user to read a number from system.in:

Scanner sc = new Scanner (system.in);

int i = Sc.nextint ();

Looking at an example, the following code enables the long type to be allocated through the items in the Mynumbers file:

Scanner sc = new Scanner (New File ("Mynumbers"));

while (Sc.hasnextlong ()) {

Long along = Sc.nextlong ();

}

The scanner can also use a separator that is different from the white space. Here is an example of reading several items from a string:

String input = "1 Fish 2 fish Red Fish Blue fish";

Scanner s = new Scanner (input). Usedelimiter ("\\s*fish\\s*");

System.out.println (S.nextint ());

System.out.println (S.nextint ());

System.out.println (S.next ());

System.out.println (S.next ());

S.close ();

The output is:

1

2

Red

Blue

The following code uses a regular expression to parse all 4 tags simultaneously and produces the same output as the previous example:

String input = "1 Fish 2 fish Red Fish Blue fish";

Scanner s = new Scanner (input);

S.findinline ("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");

Matchresult result = S.match ();

for (int i=1; I<=result.groupcount (); i++)

System.out.println (Result.group (i);

S.close ();

The default whitespace delimiter used by the scanner is identified by the character.iswhitespace.

Relative directory for file:

Know:

If a relative directory is used when creating a file, then:

Relative path names must be interpreted using information from other pathname names. By default, the classes in the Java.io package always parse relative path names based on the current user directory. This directory is specified by the system property User.dir, which is usually the calling directory for the Java virtual machine.

That is, relative to the User.dir directory. So we try not to use relative directory, here encountered, so labeled under, understand under.

Text output:

Java.io.PrintWriter

Prints the formatted representation of the object to the text output stream . This class implements all the print methods in PrintStream. It does not contain methods for writing raw bytes, and for these bytes, the program should write using an encoded byte stream.

The function of this class is to put an in-memory object, in the form of formatted text, output into the stream. Its API is as follows:

The API for Java.io.PrintStream and Java.io.PrintWriter is basically the same, except that one is output in bytes, one is a character, but this is an internal implementation.

These two classes are rarely used because System.out is using PrintStream, which we'll look at here.

Java BASIC Program Structure design basic types of input and output

Related Article

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.