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.


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

Scanner sc = new Scanner (;

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 ( ());

System.out.println ( ());

S.close ();

The output is:





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 ( (i);

S.close ();

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

Relative directory for file:


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 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:

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 and 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: 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.