Java Learning 4_ Some basic 4_ input and output _16.5.7

Source: Internet
Author: User

Read input:

To enter from the console, you first need to construct a scanner object and associate it with the standard input stream system.in.

Scanner in=new Scanner (system.in);

String Name=in.nextline (); Accept a line that may contain spaces and call the next () method if you do not want to include a space

int Age=in.nextint (); Call the Nextdouble method if you want to read a floating point number

Must be added at the beginning of the program: import java.util.*;

Formatted output:

Output the value using System.out.print (x), which prints the output x as the maximum number of non-0 digits allowed by the x corresponding data type.

The printf method in the C library function is inherited: System.out.printf ("%8.2f", X);

Input and output of the file:

To read a file, you need to construct a scanner object with the Files object:

Scanner In=new Scanner (Path.get ("MyFile.txt"));

If the file name contains a backslash, you need to add an extra backslash before the backslash: "C:\\mydirectory\\myfile.txt"

To write to a file, you need to construct a PrintWriter object. In the constructor, you only need to provide the file name:,

PrintWriter out=new printwriter ("MyFile.txt");

If the file does not exist, create the file. You can use print,println,printf like output to System.out.

Java Learning 4_ Some basic 4_ input and output _16.5.7

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.