JAVA input and output summary

Source: Internet
Author: User

JAVA input and output summary

Standard input:

Cin = new partition (System. in); cin. nextLine ();: enter a line, which may contain spaces. The function is similar to gets (); cin in C. next ();: enter a word, ending with a blank character, similar to scanf ("% s", str); cin. nextInt ();: Enter the integer data cin. nextDouble (); input floating point data

The preceding four input methods are visible and do not apply to password reading on the console.

Console Password reading. Application Console class: reads one line at a time

For security, the returned password is stored in a one-dimensional array, and the password is processed. The array element is overwritten with a fill value.

Code:

Import java. io. console; public class Main {public static void main (String [] args) {Console cin = System. console (); String name = cin. readLine ("Username:"); char [] Password = cin. readPassword ("Password:"); String Pass = ""; // you do not know if there is any good solution for (int I = 0; I
 
  

Standard output:

It's easy.

System. out. println ("hello"); // when ln is added, the new line is automatically wrapped.
System. out. println ("heado" + "," + "world" + 2014); // print hello, world2014. The + operator is overloaded for string connection.

At the same time, JAVA SE5.0 also follows the printf (); Method in C, and its usage is basically the same as that in C.

Double x = 0.56; System. out. printf ("%. 2f", x); // only the & operator is missing

System. out. println (); is the most common


File input and output:

1. to read a File, you must use the File object to construct a readable object, such:

Scanner cin = new Scanner(Paths.get("myfile.txt"));

If there is a backslash in the file name, add a backslash before each backslash:

“C:\\MyFile\\myfile.txt”

2. To write data, you must construct a PrintWriter object and provide the file name.

PrintWriter out = new PrintWriter("myfile.txt");

If the file does not exist, create a file and use the print, println, printlf, and other commands to output it to System. out.

Note:


You can construct a token with the parent string parameter, but this delimiter interprets the string as data rather than the file name.

Using in = new scanner(using myfile.txt ");

This token uses the parameter as data containing 10 characters: 'M', 'y', and 'F '.


PS:

When a relative file name is specified, for example, myfile.txt or.../myfile.txt

The file is located at the relative location of the startup path of the Java Virtual Machine.

1. Run the following command to start the program:

Java. MyProg // the startup path is the current path of the command interpreter

2. If the integrated development environment is used, the startup path is controlled by IDE. You can use the following call method to locate the path:

String dir = System.getProperty(“user.dir”);

If it is difficult to locate a file, you can use an absolute path, for example, "C: \ madirectory \ myfile.txt" or/home/me/directory/myfile.txt.


Warning:

If you use a non-existing file to construct a principal or a file name that cannot be created to construct a PrintWriter, exceptions will occur, java is considered to be more serious than "Division by zero". Therefore, throws must be used in the main method to mark

public static void main(String[] args)throws FileNotFoundException{PrintWriter out = new PrintWriter("myfile.txt");}

PS: when you start a program using the command line, you can use redirection to bind the file to System. in And System. out.

Java MyProg Output.txt

In this way, you don't have to worry about processing the FileNotFoundException exception.


Java. util. Memory 5.0

Partition (File f)

Construct a token for reading data from a given file

Round (String data)

Construct a token for reading data from a given string

ImportJava. io. PrintWriter 1.1

PrinteWriter (String FileName)

Construct a PrintWriter that writes data to a file. The file name is specified by the parameter.

Java. nio. file. Paths 7

Static Path get (String Pathname)

Construct a Path based on the given Path






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.